科尔多瓦的PhoneGap和谷歌地图V3的JavaScript API:如何点击无论许可链接或谷歌地图徽标时添加后退按钮的功能地图、徽标、科尔、按钮

2023-09-07 10:29:42 作者:寄余生

背景: 在科尔多瓦的PhoneGap 2.2应用程序运行在Android上允许听后退按钮事件

Background: The Cordova phonegap 2.2 application running on Android allows listening to the backbutton event

document.addEventListener("backbutton", function(e){ history.back();}

在谷歌地图API V3创建带有谷歌标志链接到谷歌地图网页的左下角和右下角一个可点击的链接许可证地图。标志或服务环节方面有没有具体的ID /选择。

The google maps api V3 creates the map with the Google logo linking to a Google maps webpage in the left lower corner and a clickable license link in the right lower corner. The logo or the terms of service links have no specific id/selector.

问题 当点击该网页会被重定向到一个谷歌的网页与链接:目标:_blank的网站,那么在同一个窗口作为Cordava应用程序打开,但后退按钮功能丧失,因为网页包含它自己的JavaScript。

Problem When clicking one of the links the webpage is redirected to a Google webpage with: target:_blank, the website then opens in the same window as the Cordava application, but the back button functionality is lost because the webpage contains its own Javascript.

是否有可能在加载网页注入一些code?

Is it possible to inject some code upon loading a webpage?

一个解决方案也可能会被与外部打开链接:

One solution could possibly be to open the links externally with:

navigator.app.loadUrl([href here], { openExternal:true } );

但话又说回来仍有缺乏选择的问题。

but then again there is still the problem of lack of selectors.

更新:点击按钮时,而在许可证页或谷歌地图我得到的logcat以下错误信息:11-13 16:20:30.500:E / Web控制台(31508):未捕获的Ref​​erenceError:科尔多瓦不定义:1

Update: When clicking the button while on the license page or in Google maps I get the following error message in logcat: 11-13 16:20:30.500: E/Web Console(31508): Uncaught ReferenceError: cordova is not defined:1

推荐答案

下面是相交的谷歌地图链接的一种方式。

Here is one way to intersect the google maps links.

假设你有jQuery的可用,你可以在你的脚本包含此方法:

Assuming you have jquery available, you can include this method in your script:

function directUrlToExternalBrowser(urlPattern) {
  var pattern = "a[href^='"+urlPattern+"']";//all urls startting with urlPattern
  $(pattern).live('click', function(e){
      e.preventDefault();
      navigator.app.loadUrl($(pattern).attr("href"), {openExternal: true});
  });
}

然后你可以通过下面的线直接点击到PhoneGap的API:

Then you can direct the clicks to the phonegap api by the following lines:

directUrlToExternalBrowser("http://maps.google.com/maps");
directUrlToExternalBrowser("http://www.google.com/intl");