I have an Android app which shows a number of locations on a Map. When I click on a location I would like to pass the lat long of the location, and the lat long of the device's location, to the Google Maps App so that it can show me route information between the two.
Is this possible? If so how? Thanks.
Yes, You can pass lat and lng to the Maps , and show directions to that Place using the Android Intent.
Directions are always given from the users current location.
Following query will help you perform that . You can pass the destination latitude and longitude here:
google.navigation:q=latitude,longitude
Use above as:
Uri gmmIntentUri = Uri.parse("google.navigation:q=latitude,longitude");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
More Info here: https://developers.google.com/maps/documentation/urls/android-intents
As of 2017 the recommended by Google method is the Google Maps URLs:
https://developers.google.com/maps/documentation/urls/guide#directions-action
You can create a cross-platform universal URL for Google directions following the documentation and use the URL in your intents. For example the URL might be something like
https://www.google.com/maps/dir/?api=1&destination=60.626200,16.776800&travelmode=driving
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener()
{
@Override
public void onMapClick(LatLng latLng)
{
String url = "https://www.google.com/maps/dir/?api=1&destination=" + latLng.latitude + "," + latLng.longitude + "&travelmode=driving";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With