20

How to open map intent with directions?

I know about

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format(Locale.US, "geo:%.8f,%.8f", latitude, longitude)));
startActivity(Intent.createChooser(intent, "Select an application"));

This code works fine for ONE point. But I want to show dialog with ALL map-applications, and open directions from point A to point B with any application (Google Map, Citymapper, web browser, etc).

4 Answers 4

33
+50

have you tried this

String uri = "http://maps.google.com/maps?f=d&hl=en&saddr="+latitude1+","+longitude1+"&daddr="+latitude2+","+longitude2;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(Intent.createChooser(intent, "Select an application"));
5
  • 3
    Thanks, I'll check it with other applications (1-2 days), because "Citymapper" not works for me (crash), "Ynadex Maps" (does not support this api?) not works too.
    – monyag
    Aug 13, 2014 at 18:07
  • First and best answer. +50.
    – monyag
    Aug 20, 2014 at 9:02
  • 1
    this should be surround with try/catch as it will be an exception if no activity found to handle request! Oct 27, 2015 at 10:04
  • Is there any way to display Polyline on Google Map via Intent? Apr 21, 2021 at 6:55
  • I want all location navigation based app show not just google Aug 13, 2022 at 6:17
6

try this

Uri uri = Uri.parse("geo:" + latitude1 + "," + longitude1 + "?q=" + Uri.encode(latitude2 + "," + longitude2 + "(" +Label+ ")"));
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, uri);
startActivity(intent);
2
  • Thank you very much! This was the answer that I was looking for. Lately, "maps.google.com/maps?f=d&hl=en&saddr..." opens GMaps directly even if I use Intent.createChooser
    – Adrian
    Mar 18, 2018 at 21:42
  • 1
    just load latitude2 and longitude2 as the destination and ignore first corrdinates Aug 13, 2022 at 7:10
2

Unfortunately you can't use an universal URI to represent two geo points.

You can specify a single point and pass it to the chooser like:

String uri = String.format(Locale.ENGLISH, "geo:%f,%f", lat, lng);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
context.startActivity(Intent.createChooser(intent, "Select your maps app"));

However to get directions, unfortunately, you'll need to use:

Single URL:

The best choice in my opinion. You should use a single map supplier and use its URL as the URI. That way the directions you get are consistent, compatible with multiple devices and most importantly easy to use/get.

Plain old URLs:

You can use different URL's for different map suppliers.

For example, Citymapper:

https://citymapper.com/london/superrouter?start=LAT_START,LNG_START&end=LAT_END,LNG_END

However others aren't that friendly, e.g. Yandex:

http://maps.yandex.com/?rtext=59.326460%2C12.699889~57.386826%2C12.348327&sll=12.941588%2C56.517713&sspn=34.716797%2C12.029728&rtm=atm&source=route&ll=12.941588%2C56.517713&spn=34.716797%2C12.029728&z=6&l=map

Maybe you can decode that url yourself :)

Integrated Maps API

Integrate e.g. Google Maps API into your app and show the directions from there. You would need to get directions in the format of lat-lng points from the Directions API in a JSON format. Then you would use those points to draw a polyline.

2
  • Thanks, I know it :) I need an universal method, see Foursquare
    – monyag
    Aug 14, 2014 at 0:12
  • @monyag I've checked it out and as I said it's not using an universal URI because there isn't one. It's just providing a Google Maps URL.
    – Simas
    Aug 14, 2014 at 7:42
0

If you create a URL in the format

http://maps.google.com/maps?saddr=[latitude1], [longitude1]&daddr=[latitude2], [longitude2]

Where [latitude1] and [longitude1] are the latitude and longitude for the start point, and likewise [latitude2] and [longitude2] are the end point, and set it as a String

Intent navigationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(navigationUrlOfMap));
intent.setClassName("com.Google.Android.apps.maps", "com.Google.Android.maps.MapsActivity");
 startActivity(intent); 

Or you can also use this also

startActivity(Intent.createChooser(intent, "Select an application"));

If you want to set chooser application

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.