Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map Intent With Multiple Points

I am trying to send an intent to Google Maps to show driving directions between multiple points. I am using the method listed here but it seems not to be working very well. The ultimate functionality of the app will be to create the url for the map dynamically, but I have created a static one with a bunch of random points for the sake of testing:

https://maps.google.com/maps?saddr=San+Francisco,+CA&daddr=Los+Angeles,+CA+to:Phoenix,+AZ+to:Houston,+TX+to:Jacksonville,+FL+to:New+York,+NY+to:Buffalo,+NY+to:Chicago,+IL+to:Seattle,+WA+to:San+Jose,+CA

My exact code is:

 String mapUrl = "https://maps.google.com/maps?saddr=San+Francisco,+CA&daddr=Los+Angeles,+CA+to:Phoenix,+AZ+to:Houston,+TX+to:Jacksonville,+FL+to:New+York,+NY+to:Buffalo,+NY+to:Chicago,+IL+to:Seattle,+WA+to:San+Jose,+CA";
 Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(mapUrl));
 startActivity(i);

If I send this to the browser from the "Complete action using" menu, then it works correctly. (FWIW, it also works correctly if I just copy+paste the url into a web browser on my desktop) On the other hand, if I send it to the Maps application, then it does not behave correctly. On some of the older devices that I have running 2.2 and 2.3, it shows driving directions but only 2 of the points are marked, one of which not even being one of the points I passed in the url, and the driving directions are crazy with multiple routes going back and forth between locations. On any 4.X device, it displays nothing and either says "Network failure" or "No route found". I do not have any 3.X devices however I expect it would behave in a similar fashion to one of these.

I do not want to use a MapView as this is pretty overkill for the minor map-based requirements of my app. I just want to be able to send an intent to the Maps app showing a route between two locations with waypoints in between. I could not find any clear answers in the documentation for the new Maps api, is this possible?

like image 808
Dave Avatar asked Oct 24 '13 18:10

Dave


2 Answers

Use this code:

Uri gmmIntentUri = Uri.parse("https://www.google.com/maps/dir/?api=1&origin=18.519513,73.868315&destination=18.518496,73.879259&waypoints=18.520561,73.872435|18.519254,73.876614|18.52152,73.877327|18.52019,73.879935&travelmode=driving");
Intent intent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
intent.setPackage("com.google.android.apps.maps");
try {
    startActivity(intent);
} catch (ActivityNotFoundException ex) {
    try {
        Intent unrestrictedIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
        startActivity(unrestrictedIntent);
    } catch (ActivityNotFoundException innerEx) {
        Toast.makeText(this, "Please install a maps application", Toast.LENGTH_LONG).show();
    }
}

More info in this link: https://developers.google.com/maps/documentation/urls/guide

like image 86
Mayur Misal Avatar answered Nov 12 '22 08:11

Mayur Misal


final String uri = "http://maps.google.com/maps?daddr=Gachibowli"+"+to:Lingampally+to:Nizampet+to:kukatpally+to:moosapet+to:Nizampet+to:srnagar+to:ameerpet+to:jubileehills+to:kothaguda";
final Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
    startActivity(intent);
}

If you want to use lat lng modify like this:

final String uri = "http://maps.google.com/maps?daddr=12.972442,77.580643"+"+to:12.9747,77.6094+to:12.9365,77.5447+to:12.9275,77.5906+to:12.9103,77.645";
final Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
if (intent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
    startActivity(intent);
}

*Using "+to" you can add multiple points.

like image 1
vikas naik Avatar answered Nov 12 '22 08:11

vikas naik