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

androidgoogle mapsiphonemobile

I have a mobile site and I have a link that can 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.

Best Answer

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.