开发者

open google maps app from a browser with default start location on android and iphone

I have a mobile site and I have a link that c开发者_JS百科an open the google maps native app on iphone and android with default start and end locations by using the link format: http://maps.google.com/maps?saddr=XX&daddr=XX.

My problem is, I want to be able to set the starting location to be the current location, and I want it to work from both android and iphone browsers. From my experience, if I leave the saddr blank, in android this defaults to the current location but on iphone it just leaves the start location blank. If I set the saddr to "current location", this sets the start location to be the current location on iphone but on android it doesn't recognize the location.

My solution right now is to use the user's geo lat/long coordinates as the start address. This works successfully across both platforms but depending on the method used to get the coordinates, it may not be as accurate as the user's current location from the gps on the app. I was wondering if there was some way to get the link to open the native app with the correct "current location" for the starting point. I know I could also conditionally check for the user-agent and create the link accordingly but I was hoping that there would be a more elegant way to do this.


I simply replace the values in the following string with my start and destination Lng/Lat

http://maps.google.com/maps?saddr=START_ADD&daddr=DEST_ADD&ll=START_ADD

Then I launch the url like this in android, which will let the user choose to either use the browser or their native maps app:

String url = "http://maps.google.com/maps?saddr=START_ADD&daddr=DEST_ADD&ll=START_ADD";

startActivity( new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url)));  

For iOS, if you prefix the url with maps:// instead of http:// it will launch the maps application on the phone which works really well. Something like

NSString *url = @"maps://maps.google.com/maps?saddr=START_ADD&daddr=DEST_ADD&ll=START_ADD";

[[UIApplication sharedApplication] openURL:[NSURL urlWithString:url]];

That is the way I have chosen to handle it. Its pretty easy on either device to get the current Lng/Lat using their respective location frameworks. The results always seem pretty accurate to me as well, you can specify in either how accurate you want the results. The more accurate, the more drain on your battery and sometimes it takes longer to acquire but that's usually something you tweak in the end to get the best results for your implementation.


Take a look at the url handler geo to tell Browser to use native App

<a href="geo:50.066274, 10.754427;">Location here!</a>

There is a polyfill script available which would be worth a try for fallback for Desktop Browsers: https://github.com/prowestgis/dojo-geo-uri-polyfill

You may try also for IOs devices those:

comgooglemaps://?parameters

or:

comgooglemaps-x-callback://?parameters

https://developers.google.com/maps/documentation/urls/ios-urlscheme


I know I could also conditionally check for the user-agent and create the link accordingly but I was hoping that there would be a more elegant way to do this.

This IS the most elegant solution. The two platforms differ too much, to do this with the same link for both platforms. Add Blackberry or Windows Phone in the equation, and you will have even more difficulties in achieving the same behaviour without checking the user-agent


In Iphone you can get the current location by using CLLocationManagerDelegate in the following method.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

    CLLocationCoordinate2D location = newLocation.coordinate;
    Float x = location.latitude, y = location.longitude;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜