Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, how to pass lat long route info to Google Maps App

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.

like image 631
MHugh Avatar asked Mar 08 '17 17:03

MHugh


2 Answers

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

like image 135
WannaBeGeek Avatar answered Oct 15 '22 10:10

WannaBeGeek


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!

like image 4
xomena Avatar answered Oct 15 '22 10:10

xomena