Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto start navigation with React Native Google Maps Directions package

I have a requirement that on loading the google map application, have to automatically start the navigation.

Current scenario - It shows the route but user has to click on start to start navigation I couldn't find any flag associated for the same?

Found this article which shows the flags used in google maps

Official google maps docs shows to use it as dir_action=navigate but I am clueless on how to translate it into react-native-google-maps-directions package

Any idea how could we approach the problem?

like image 219
Aseem Upadhyay Avatar asked Mar 14 '18 08:03

Aseem Upadhyay


People also ask

Is google Directions API free?

The Directions API uses a pay-as-you-go pricing model. Directions API requests generate calls to one of two SKUs depending on the type of request: basic or advanced. Along with the overall Google Terms of Use, there are usage limits specific to the Directions API.


2 Answers

The react-native-maps-directions uses the Directions API to display routes. Please note that Directions API doesn't include any real-time navigation, it is meant only to show routes. The real-time navigation additionally is prohibited in the Google Maps APIs.

Have a look at the Google Maps API Terms of Service paragraph 10.4 (c, iii). It reads

No navigation. You will not use the Service or Content for or in connection with (a) real-time navigation or route guidance; or (b) automatic or autonomous vehicle control.

source: https://developers.google.com/maps/terms#10-license-restrictions

In order to be compliant with Google Maps API Terms of Service you should open the Google Maps app installed in your device using the Google Maps URLs in navigation mode.

var url = "https://www.google.com/maps/dir/?api=1&travelmode=driving&dir_action=navigate&destination=Los+Angeles";
Linking.canOpenURL(url).then(supported => {
    if (!supported) {
        console.log('Can\'t handle url: ' + url);
    } else {
        return Linking.openURL(url);
    }
}).catch(err => console.error('An error occurred', err)); 

I hope this helps!

like image 182
xomena Avatar answered Oct 12 '22 17:10

xomena


100% work

If you want to auto-start navigation from your current location to specific location https://developers.google.com/maps/documentation/urls/android-intents#launch_turn-by-turn_navigation

Sample Code

static googleMapOpenUrl = ({ latitude, longitude }) => {
    const latLng = `${latitude},${longitude}`;
    return `google.navigation:q=${latLng}`;
  }

To open google map on click React-Native will do like this

Linking.openURL(googleMapOpenUrl({ latitude: 23.235899, longitude: 78.323258 }));
like image 4
Vijay Chouhan Avatar answered Oct 12 '22 16:10

Vijay Chouhan