I am developing an Android app where I am using Google Maps. It is working well and good. But
After loading the map when a user has clicked “Get Directions”, Google
This should be of help Routing / Driving directions on Android by mobile.synyx.de
read -> Getting the geopoints from google maps
Here is a solution.
The below Intent will request turn-by-turn navigation to Taronga Zoo, in Sydney Australia from your current GPS location
Uri gmmIntentUri = Uri.parse("google.navigation:q=Taronga+Zoo,+Sydney+Australia");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
You can give latitude and longitude instead of the location name.
Uri gmmIntentUri = Uri.parse("google.navigation:q=-33.846346,151.239518");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
Check here for more information.
The best way to get direction and routes you can use the Web Service of Google Maps. It will provide you everything. I have used this in my application.
Here is the example where saddr = source address & daddr= destination address(i.e. latitude & longitude). You can also pass string as address instead of lat/lng.
final Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://maps.google.com/maps?" + "saddr="+ latitude + "," + longitude + "&daddr=" + latitude + "," + longitude));
intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
startActivity(intent);
Hope this will help you.
As of 2018, Google has announced the Google Maps Navigation SDK. You can see the following Google Maps Platform presentation at I/O'18 where Google Maps Product Managers mentioned the SDK:
https://youtu.be/XVjyIA3f_Ic?t=20m31s
Unfortunately, this SDK is not available publicly at the moment. You have to contact sales team in order to purchase license for this SDK. As I can see currently only big ride sharing companies like Uber or Lyft have this SDK in their apps.
Alternatively, you can open Google Maps app in navigation mode using the Google Maps URLs. Google Maps URLs build a universal, cross-platform URL to launch Google Maps, so you can use them with your intents.
E.g.
String url = "https://www.google.com/maps/dir/?api=1&destination=Madrid,Spain&origin=Barcelona,Spain&travelmode=driving&dir_action=navigate";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
Adding onto Scorpion's code (which works perfectly) you might want to get your current location
Location currentLocation = googleMap.getMyLocation();
double latitudeCurr = currentLocation.getLatitude();
double longitudeCurr = currentLocation.getLongitude();
saddr
being starting address
daddr
being destination address
final Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("+http://maps.google.com/maps?" "saddr="
+ latitudeCurr + "," + longitudeCurr + "&daddr="
+ latitude + "," + longitude));
intent.setClassName("com.google.android.apps.maps",
"com.google.android.maps.MapsActivity");
startActivity(intent);
This is what I use in my application