使用React Native Google Maps Directioning软件包自动启动导航



我有一个要求,在加载谷歌地图应用程序时,必须自动启动导航。

当前方案 - 它显示路线,但用户必须单击start才能开始导航我找不到任何与之相关的标志?

找到这篇文章,其中显示了谷歌地图中使用的标志

官方谷歌地图文档显示将其用作dir_action=navigate但我对如何将其翻译成react-native-google-maps-directions包一无所知

知道我们该如何解决这个问题吗?

react-native-maps-direction 使用 Directions API 来显示路线。请注意,路线 API 不包含任何实时导航,它仅用于显示路线。此外,Google Maps API 中还禁止实时导航。

请查看 Google Maps API 服务条款第 10.4 (c, iii( 段。上面写着

没有导航。您不得将服务或内容用于或与以下方面相关:(a( 实时导航或路线指导;或 (b( 自动或自动驾驶汽车控制。

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

为了符合谷歌地图API服务条款,您应该在导航模式下使用谷歌地图网址打开安装在您设备中的谷歌地图应用程序。

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

我希望这有帮助!

100% 工作

如果要自动启动从当前位置到特定位置的导航 https://developers.google.com/maps/documentation/urls/android-intents#launch_turn-by-turn_navigation

示例代码

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

要打开谷歌地图,请单击 反应原生将这样做

Linking.openURL(googleMapOpenUrl({ latitude: 23.235899, longitude: 78.323258 }));
    import getDirections from "react-native-google-maps-directions";
    ...
    handleGetDirections = () => {
        const data = {
            source: {
                latitude: originLatitude,
                longitude: originLongitude
            },
            destination: {
                latitude: destinaationLatitude,
                longitude: destinationLatitude
            },
            params: [
                {
                    key: "travelmode",
                    value: "driving"  // may be "walking", "bicycling" or "transit" as well
                },
                {
                    key: "dir_action",
                    value: "navigate" // this instantly initializes navigation using the given travel mode
                }
            ]
        }
        getDirections(data)
    }

来源: https://www.npmjs.com/package/react-native-google-maps-directions#install

android/app/src/main/AndroidManifest.xml添加意向(自 Android 11 SDK 30 以来的一项新要求(:

<intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="google.navigation" />
</intent>

使用带有通用跨平台网址的Linking.openURL来启动 Google 地图,例如,以下示例启动一张地图,其中包含从华盛顿州西雅图的 Space Needle 到 Pike Place Market 的自行车路线:

Linking.canOpenURL(url).then((supported) => {
    if (supported) {
      Linking.openURL(`https://www.google.com/maps/dir/?api=1&origin=Space+Needle+Seattle+WA&destination=Pike+Place+Market+Seattle+WA&travelmode=bicycling`);
    } else {
      logger.error("Don't know how to open URI: " + url);
    }
  });

来源

  • 安卓意图 - https://stackoverflow.com/a/65114851/3469524
  • Linking.openURL - https://stackoverflow.com/a/43804295/3469524
  • 用于启动谷歌地图的通用跨平台网址

最新更新