Android: open map intent using two-dot directions

How to open a map 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 great for ONE point. But I want to show a dialogue with all mapping applications and open directions from point A to point B with any application (Google Map, Citymapper, web browser, etc.).

+6
source share
4 answers

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")); 
+19
source

Unfortunately, you cannot use a universal URI to represent two geo-points.

You can specify one point and pass it to the selector, for example:

 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 will need to use:

Single URL:

The best choice, in my opinion. You must use one map provider and use its URL as a URI. Thus, your directions are consistent, compatible with several devices and, most importantly, easy to use / receive.

Plain old urls:

You can use different URLs for different map providers.

For example, Citymapper:

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

However, others are not so friendly, for example. 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 

Perhaps you can decode this url yourself :)

Integrated Maps API

Integration, for example. Google Maps API into your application and show directions from there. You will need to get the instructions in Latin dot format from the Directions API in JSON format. You will then use these points to draw the polyline.

+1
source

try it

  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); 
+1
source

If you create a URL in the format

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

If [latitude1] and [longitude1] are the latitude and longitude for the starting point, as well as [latitude2] and [longitude2] are the ending 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 as well

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

If you want to install the application of choice

0
source

Source: https://habr.com/ru/post/973511/


All Articles