18

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 Maps comes up with the direction line, however there is no way to get the turn by turn written directions. If you just open Google Maps and Get Directions you can toggle back and forth between the Map and the Direction List.

Is there any API available to get all the features as given in default Google map of Android device?

Andriy M
  • 76,112
  • 17
  • 94
  • 154
Sudipta Som
  • 6,537
  • 9
  • 43
  • 67

5 Answers5

40

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.

Scorpion
  • 6,831
  • 16
  • 75
  • 123
  • 1
    so is just opens Google Map app? We can't create UI in our own app using this api? – user25 Feb 17 '17 at 10:05
  • 1
    If you want to create UI in your own app with navigation then You have to utilise Google's Direction API and integrate it with map in your code. – Scorpion Feb 27 '17 at 07:43
  • This works only if Google Maps app is installed on device. This app may not be present, for example on newer Huawei devices, or when user uninstalls Maps app. So this answer doesn't cover all user cases. – Pointer Null Aug 12 '20 at 16:55
8

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

Paul Richter
  • 10,908
  • 10
  • 52
  • 85
Havnar
  • 2,558
  • 7
  • 33
  • 62
4

This should be of help Routing / Driving directions on Android by mobile.synyx.de

read -> Getting the geopoints from google maps

Ujwal Parker
  • 682
  • 7
  • 11
3

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);
xomena
  • 31,125
  • 6
  • 88
  • 117
1

In order to get the (current) navigation turn-by-turn written directions, the only way is to parse the notifications so far.

To handle this I've written an open source library that may help you in the process (but it needs notifications access): GMapsParser

You can get the state with a service doing something like:

class NavigationListenerEmitter : NavigationListener() {
    override fun onNavigationNotificationAdded(navNotification: NavigationNotification) {
        Log.d("notificationListener", "Got initial notification $navNotification")
    }

    override fun onNavigationNotificationUpdated(navNotification : NavigationNotification) {
        Log.d("notificationListener", "Got notification update $navNotification")
    }

    override fun onNavigationNotificationRemoved(navNotification : NavigationNotification) {
        Log.d("notificationListener", "Removed notification $navNotification")
    }
}

It also provides an utility class to expose such events as parcels that can be easily passed to activities.

Treviño
  • 2,999
  • 3
  • 28
  • 23