Open Google maps app with directions by coordinates
Asked Answered
M

2

5

I'm coding an app (both iOS and Android) that should be able to open the Google Maps app on the device and give directions to a target. The target is provided as latitude/longitude coordinates. I have read the official guid how to do this (https://developers.google.com/maps/documentation/urls/guide) but it still doesn't work.

Below is an example of an url that I send from my app. If I paste that same url into a browser on my computer, it works fine, but if I try to open it on my device I get an error message that says "Unsupported link, Google Maps can't open this link".

https://www.google.com/maps/dir/?api=1&destination=50.693907573202%2C10.970328366756

What am I doing wrong?

EDIT: I noticed that it works the second time i send the url, but never the first. In other words: in my app, I touch the "Directions" button and Google Maps is displayed with the error message. I then return to my app and press the "Directions" button again and it works. Why does it not work the first time? Does Google Maps need to be running in the background for it to work? There is no mention of this in the docs...

Mousy answered 12/1, 2018 at 10:58 Comment(1)
Although the docs state that the origin "defaults to most relevant starting location" I added the origin and now it works every time. It seems that it took one "startup" for Google Maps to get the current location and thereby letting the subsequent tries go well.Mousy
T
7

You have to use this to start navigation on mobile plateform.

Uri gmmIntentUri = Uri.parse("google.navigation:q=28.5675,77.3260");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);

For futher reading https://developers.google.com/maps/documentation/urls/android-intents

Timekeeper answered 12/1, 2018 at 11:9 Comment(6)
Thanks for your quick answer! However, the error occurs on both iOS and Android. Also, in the docs that I am referring to, it says "Using Maps URLs, you can build a universal, cross-platform URL to launch Google Maps and perform searches, get directions [...] . The URL syntax is the same regardless of the platform in use." To me, this indicates that these urls should be enough. If not, when/how can these "universal, cross-platform URLs" be used at all?Mousy
On the page you refered to, I also found this: "It is recommended that you use the cross-platform Maps URLs to launch Google Maps, since these universal URLs allow for broader handling of the maps requests no matter which platform the user is on. You should only use the Android-specific Maps Intents for features that may only be functional on a mobile platform." It seems that Google themselves recommend the "regular" urls.Mousy
use it this way google.com/maps/dir/…Timekeeper
That link is identical to mine except that you haven't url-encoded the comma (which Google clearly specifies in the docs should be done).Mousy
I am using it in the way i have mentioned and it is working fineTimekeeper
I tried it without url-encoding the comma but I still get the same error.Mousy
E
0

Use google direction API ,you can easily integrate Google map direction API,Follow below link - https://www.journaldev.com/13373/android-google-map-drawing-route-two-points

Epagoge answered 12/1, 2018 at 11:15 Comment(1)
Thanks for your reply. However, the error occurs on both platforms, not just Android. I need a solution that works for both (not coding native). Moreover, following the instructions given by Google themselves on how to interface with their Maps app should be enough. I just want to know what I have misunderstood there...Mousy

© 2022 - 2024 — McMap. All rights reserved.