5

I have many latitude longitude in my android app. I want to know how to open a chooser or Google map App through intent and show direction from current location to that latitude and longitude.Like i have lat=28.605989,lon=77.372970 and my current location is somewhere so when i click on button in my app these lat lon pass through intent and will show direction from my current location to these lat lon. Thank you.

Gourav Manuja
  • 89
  • 1
  • 10

2 Answers2

12

To navigate user to destination lat long, we can use something like this

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("http://maps.google.com/maps?daddr=28.605989,77.372970"));
startActivity(intent);

Here . we are passing just destination lat long daddr=28.605989,77.372970, so by default, your current location will be source location.

To add extra information with your url .

String my_data= String.format(Locale.ENGLISH, "http://maps.google.com/maps?daddr=20.5666,45.345(My Destination Place)");

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(my_data));
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51
  • @GouravManuja sure thing. maek sure you have installed google map in your phone . otherwise you have to handle `ActivityNotFound` Exception – Tejas Pandya Sep 13 '18 at 09:42
  • sir i m trying your code but my studio says not responding that's why its take so longer but i want to know one more thing...can i pass info window through intent on google map ??? – Gourav Manuja Sep 13 '18 at 09:54
  • like i want to show some information about location but i m using google map through intent so i want to know can i show my information on google map??? – Gourav Manuja Sep 13 '18 at 09:55
  • @GouravManuja . for information im updating my answer – Tejas Pandya Sep 13 '18 at 09:58
  • sir you are great,done current location to end points,but suppose i have lat lon of a doctor,i want to show the name of doctor the no of doctor and the time to meet him...so explain how can i show ??? – Gourav Manuja Sep 13 '18 at 10:09
  • sir i am passing data like this but where would show data which i passed on maps??? – Gourav Manuja Sep 13 '18 at 10:14
  • Sir String is not showing anywhere in map...where can i see that string which i pass in map ?? – Gourav Manuja Sep 13 '18 at 10:18
  • you can modify your string like this . `String my_data= String.format(Locale.ENGLISH, "http://maps.google.com/maps?daddr=20.5666,45.345",20.5666,45.345, "Doctor Name \n 10 docs \n 12:30PM");` – Tejas Pandya Sep 13 '18 at 10:19
  • Sir i cant see anywhere...firstly i want to know where can i see these all data on map....how can i see...i passed data hardcoded but it is not visible anywhere...where is it visible?? – Gourav Manuja Sep 13 '18 at 10:22
  • You have to encode your string `Uri.encode() `. please visit [This](https://developers.google.com/maps/documentation/urls/android-intents) link . see URI encoded query strings part .you will get clear idea . – Tejas Pandya Sep 13 '18 at 10:24
  • sir i encode my string Uri.encode("1st & Pike, Seattle") like this...but i m not able to see data anywhere... is it okay to unicode string ??? where would show my data ??? – Gourav Manuja Sep 13 '18 at 10:39
  • @GouravManuja updated my answer . pass with `lat,long(label)` – Tejas Pandya Sep 13 '18 at 11:01
  • Sir where can i see my string on map ?? – Gourav Manuja Sep 13 '18 at 12:31
  • @GouravManuja on the destination address pin – Tejas Pandya Sep 13 '18 at 12:32
  • sir i can't share the image of map...here is showing nothing...which string i passed – Gourav Manuja Sep 13 '18 at 12:39
  • @TejasPandya Is there any official documentation of this URL which you added as String in Uri.parse? – Ssubrat Rrudra Feb 01 '20 at 09:10
  • @MustufaAnsari I would suggest you to please go through this [answer](https://stackoverflow.com/a/46910653/6880611). – Tejas Pandya May 18 '20 at 06:45
2

You can send intent to Google Map android application by following code:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("http://maps.google.com/maps?saddr=22.458,34.34&daddr=75.124,35.448"));
startActivity(intent);

In the above URI saddr represents the source address i.e. your current location and daddr represents the coordinates of the destination location.

Ankit Mehta
  • 4,251
  • 4
  • 19
  • 27
  • i think u define here your current location but i don't have current location i just have end points so what should i do for current location bcz i can be anywhere so this is not fix i want from my current location to my end points which i have,show direction – Gourav Manuja Sep 13 '18 at 09:22
  • eventually, there is no such intent URI provided according to android documentation. you need to fetch current co-ordinates before redirecting to the google map. – Ankit Mehta Sep 13 '18 at 09:29
  • you mean i should find out current location and pass with saddr and end points pass with daddr ? through intent in map i can't find my current location? – Gourav Manuja Sep 13 '18 at 09:31
  • you can try the answer submitted by Tejas. if it doesn't work then you need to retrieve the current location first. try passing only the destination co-ordinates. – Ankit Mehta Sep 13 '18 at 09:31
  • sir i m trying your code but my studio says not responding that's why its take so longer but i want to know one more thing...can i pass info window through intent on google map ??? like i want to show some information about location but i m using google map through intent so i want to know can i show my information on google map??? – Gourav Manuja Sep 13 '18 at 09:56