问题描述
我的问题类似于How to open a mobile device's map app when a user clicks on a link?。
我有一个使用 angular 8 开发的应用程序,它在桌面浏览器、移动浏览器和一个使用基于铬的 Cordova(使用 Android 浏览器)的安卓应用程序中运行,以显示网络视图(iframe)。
我在我的 angular 应用程序中使用了 angular google map 并且我正在尝试实现类似的东西,如果用户在移动浏览器或 andriod 应用程序(使用cordova web 视图)中打开我的应用程序,我需要打开本机/inbuilt google map 用于在单击链接时导航指定来源和目的地。在桌面上,我打开了谷歌地图网络视图。
这是我的代码如下:
navigate(startAddress,destAddress){
if (navigator.userAgent.match(/(Android|BlackBerry|IEMobile)/) && !this.inIframe()) {
window.open(`geo:${this.latitude},${this.longitude}?q=${startAddress},${destAddress}`);
}else if ((navigator.platform.indexOf("iPhone") != -1)) {
window.open(`http://maps.apple.com/?q="${destAddress}`,'_system')
}else {
window.open(`https://www.google.com/maps/dir/?api=1&travelmode=driving&layer=traffic&origin=${startAddress}&destination=${destAddress}`);
}
}
inIframe() {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
但是我在这里遇到了一个问题,当我确定我的应用程序是在移动设备中打开时(第一个 if 语句),我使用 geo:${this.latitude},${destAddress}
打开内置的谷歌地图导航。内置的谷歌地图被打开,但它没有直接显示给定来源和目的地的导航。
我不确定如何将源地址和目标地址geo:
正确地与 src 和 dest 地址一起使用。
我看到 Android - launch google map via web url 帮助我直接打开内置谷歌地图,但使用我的方法和开始和结束地址,应用程序被冻结,也不会直接显示导航。 您能否验证我的方法并让我知道我哪里出错了?
解决方法
在 Android 设备上:
- 如果 Android 版 Google 地图应用已安装并处于活动状态,则该网址会在地图应用中启动 Google 地图并执行请求的操作。
- 如果未安装或禁用 Google 地图应用,则该网址会在浏览器中启动 Google 地图并执行请求的操作。
上面是正常的请求,在你的代码中看起来也不错,所以你也可以使用 android 的链接实现。
除此之外,您可能还想了解 url 编码和构造栏:https://developers.google.com/maps/documentation/urls/url-encoding
,省去麻烦并使用https://github.com/dpa99c/phonegap-launch-navigator为您做到这一点