使用 Phonegap 7.0.1 打开 Google 个地图

Open Google Maps with Phonegap 7.0.1

对于之前的 Phonegap 版本,我过去常常从应用程序中打开 google 地图,只需使用 href 和 url 打开,就像这样 https://www.google.com/maps/dir/?api=1&origin=43.9815648,7.5328161&destination=41.802425,12.6021389

但我注意到,在 Phonegap 7 中,当我点击 href 元素时,没有任何反应。为什么?

如何解决此问题并打开具有给定行程的 google 地图?

出于安全原因,您无法将 Cordova/PhoneGap 视图重定向到其他地方托管的页面(相对于您的 phone),因此您有两个选择:

  1. 为 Google 地图使用 Cordova/PhoneGap 插件,例如: https://www.npmjs.com/package/cordova-plugin-googlemaps

  2. 使用 Cordova/PhoneGap 应用内浏览器插件启动您的地图 在您的应用中的全屏浏览器 window 中: https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-inappbrowser/

选项 #1 是首选。

如果您想启动 Google 地图应用(而不是在您的应用中嵌入 Google 地图,即 cordova-plugin-googlemaps), you can use the phonegap-launch-navigator 插件。

您可以加​​载的页面、您可以加载的脚本等现在由 CSP(内容安全策略)控制,而不仅仅是 config.xml 中的旧白名单机制。因此,如果您想访问页面,您必须设置 Content Security Policy appropriately. To use Google maps you at least need to add google.com to the default src, gstatic.com to data. These may not be enough, and if you they aren't probably the only option is looking at the errors in the developer console, see here how to get Chrome Developers console on Android, and here 才能在 iOS 上看到它。我总是发现需要反复试验才能将 CSP 设置得恰到好处。

<meta http-equiv="Content-Security-Policy" content="default-src 'self' google.com data: gap: https://ssl.gstatic.com; style-src 'self' 'unsafe-inline' google.com; media-src *">

就是说,您可能不希望 Google 地图控制您的应用程序(或者您可能这样做?),因此会推荐使用 In App Browser 插件的其他建议。它只是添加一个 plugin 并使用一些 javascript 打开 window:

cordova.InAppBrowser.open('https://www.google.com/maps/dir/?api=1&origin=43.9815648,7.5328161&destination=41.802425,12.6021389', '_blank', 'location=yes');