Sudipta Som
Sudipta Som

Reputation: 6577

Is there an API for Google Maps navigation in Android?

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?

Upvotes: 18

Views: 46457

Answers (6)

theprof
theprof

Reputation: 1167

As of Sep 2024, you can now sign up for the Google Nav SDK without having to contact sales.

1/ Sign into Google Cloud Console and create a new GCP project (you might need to add a valid credit card to enable billing).

2/ Click the [+ Enable APIs and Services] and search for the "navigation sdk". Press [ENABLE] to bind the Nav SDK to your project.

3/ Return to the project dashboard and click on [+ Create Credentials]. Your new API key can be retrieved by clicking on the [Show Key] button on the right.

4/ Follow the these steps (https://developers.google.com/maps/documentation/navigation/android-sdk/android-studio-setup) to set up your API key to run the Android Nav SDK starter code (https://github.com/googlemaps-samples/android-navigation-samples)

Link: https://www.afi.io/blog/google-odrd-navigation-sdk/

Upvotes: 0

Treviño
Treviño

Reputation: 3568

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.

Upvotes: 1

xomena
xomena

Reputation: 32178

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);

Upvotes: 3

Havnar
Havnar

Reputation: 2638

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

Upvotes: 8

Scorpion
Scorpion

Reputation: 6901

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.

Upvotes: 40

Ujwal Parker
Ujwal Parker

Reputation: 676

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

read -> Getting the geopoints from google maps

Upvotes: 1

Related Questions