NonowPoney
NonowPoney

Reputation: 992

Intent to Google Navigation for itinerary with several waypoints

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.

Upvotes: 11

Views: 17188

Answers (4)

Bhagvati Vekariya
Bhagvati Vekariya

Reputation: 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);

Upvotes: -1

kushyar
kushyar

Reputation: 1191

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); 

Upvotes: 21

Ayaz Alifov
Ayaz Alifov

Reputation: 8608

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);

Upvotes: 3

user2426981
user2426981

Reputation: 11

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.

Upvotes: 1

Related Questions