Intent to Google Navigation for itinerary with several waypoints
Asked Answered
H

4

11

I want to know, if it's possible to put several "checkpoints" within "Google Navigation" (Google Maps Navigation) With a query that follows the next syntax : "google.navigation:q=44.871709,-0.505704...."

If it possible, what is the syntax for separate two points ?

For one point it works, example : "google.navigation:q=44.871709,-0.505704"

But I would put a few checkpoints for example :

 LatLng point1 = new LatLng(44.871709,-0.505704);
 LatLng point2 = new LatLng(43.572665,3.871447);                    
 Intent(android.content.Intent.ACTION_VIEW,Uri.parse("google.navigation:q="+
 point1.latitude+","+point1.longitude+";"+ point2.latitude+","+point2.longitude));  

I read others issues, they say to use this syntax :

"http://maps.google.com/maps?saddr="+"44.871709,-0.505704"+"&daddr="+"43.572665,3.871447"

With above solution, "Google Navigation" is not started automatically, we must choose one application "Google Maps" and next, to click on Itinerary (in top of the screen) to start "Google Navigation". I would like to avoid it if possible.

Hendel answered 23/5, 2013 at 15:11 Comment(0)
R
21

Here is a way to supress the dialog and go to maps directly.

String uri = "http://maps.google.com/maps?saddr="+"44.871709,-0.505704"+"&daddr="+"43.572665,3.871447";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent);

Let me know if you still have issues.

UPDATE

As of May 2017, there is a better approach, the Google Maps URLs API

https://developers.google.com/maps/documentation/urls/guide

Using this API you can construct an URL with origin, destination and waypoints and pass it to the intent

String uri = "https://www.google.com/maps/dir/?api=1&origin=Madrid,Spain&destination=Barcelona,Spain&waypoints=Zaragoza,Spain%7CHuesca,Spain&travelmode=driving&dir_action=navigate";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
startActivity(intent); 
Rodmur answered 28/5, 2013 at 4:43 Comment(5)
I think I'll give up my idea and keep your solution thank you ;-)Hendel
Is there any way to add features like "Avoid Highways" in this URL?Reichsmark
Is there any way to add flag to cancel any ongoing navigation?Pettiford
what if GoogleMaps not installed on device?Woodworth
can i put onActivityResult on this intent ? because i want kilometer from that live navigation.Chicanery
L
3

Check these 2 methods, both of them work properly for me.

METHOD 1:

    String packageName = "com.google.android.apps.maps";
    String query = "google.navigation:q=49.4839509,8.4903999";

    Intent intent = this.getPackageManager().getLaunchIntentForPackage(packageName);
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(query));
    startActivity(intent);

METHOD 2:

    String packageName = "com.google.android.apps.maps";
    String query = "google.navigation:q=49.4839509,8.4903999";

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(query));
    intent.setPackage(packageName);
    startActivity(intent);
Lantz answered 20/4, 2016 at 13:57 Comment(0)
P
1

Because point1.longitude is Double,you should use this

String.valueOf(point1.latitude);

Intent(android.content.Intent.ACTION_VIEW,Uri.parse("google.navigation:q="+String.valueOf(point1.latitude)+","+String.valueOf(point1.longitude)+";"+String.valueOf( point2.latitude)+","+String.valueOf(point2.longitude)));  

Or you can use this,but it is just the same.

point1.toString().replace("lat/lng: (", "").replace(")", "")

point1.toString() the result is "lat/lng: (44.871709,-0.505704)",remove the "lat/lng: (" and ")". then you can get the result that you want.

Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr="+point1.toString().replace("lat/lng: (", "").replace(")", "")+"&daddr="+point2.toString().replace("lat/lng: (", "").replace(")", ""))); 

Or you can try this

Uri uri = Uri.parse("http://maps.google.com/mapssaddr="+point1.toString().replace("lat/lng: (", "").replace(")", "")+"&daddr="+point2.toString().replace("lat/lng: (", "").replace(")", ""));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

I hope this can help you.

Politburo answered 28/5, 2013 at 4:30 Comment(2)
I try this but GoogleNav. still does not understand this itinerary. Thank you anyway for your help ;-)Hendel
in google.navigation:q= doesn't understand ; between multiple latlngOpportina
R
-1

You have to just pass destination location. Google Maps app will automatically take your current location for navigation.

Intent intent = new Intent(android.content.Intent.ACTION_VIEW,  Uri.parse("google.navigation:q=" + String.valueOf(destination latitude)
                        + "," + String.valueOf(destination longitude)));

startActivity(intent);
Ratsbane answered 30/10, 2015 at 6:46 Comment(1)
the question is about specifying multiple locations, not one.Ulla

© 2022 - 2024 — McMap. All rights reserved.