如何防止 Phonegap 应用程序中的地理定位双重提示?

时间:2023-03-21
本文介绍了如何防止 Phonegap 应用程序中的地理定位双重提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我为 iPhone 创建了一个 PhoneGap 应用,它通过 webview 中的 JavaScript 使用地理定位.

I created a PhoneGap app for iPhone that uses geolocation via JavaScript inside webview.

当我第一次运行该应用程序时,它会提示我允许此应用程序的地理定位.

When I run the app the first time, it'll prompt me to allow geolocation for this app.

当我点击ok"时,它会再次提示我同样的问题,但这次它指出index.html"需要使用地理位置的权限.

When I hit "ok", it'll prompt me again with the same question but this time it states that "index.html" wants permission to use geolocation.

这是有道理的,因为 iOS 可能第一次需要允许应用程序本身的地理定位权限,而浏览器第二次需要权限.

That makes sense because iOS probably wants permission to allow geolocation for the app itself for the first time and the 2nd time the browser wants permission.

但是,因为不会带来出色的用户体验:

However, since doesn't lead to a great user experience:

如何防止这种双重提示?(如果可以防止第二个提示就足够了)

How can I prevent this double prompt? (I'd be enough if the 2nd prompt could be prevented)

推荐答案

我找到了问题的原因.

navigator.geolocation.getCurrentPosition(onsuccess, onerror) 的调用发生在 Phonegap 完全加载之前.

The call to navigator.geolocation.getCurrentPosition(onsuccess, onerror) happens before Phonegap was fully loaded.

这意味着 webview 的地理定位调用(而不是通过 PhoneGap 的本地调用)被触发,这将再次请求许可(这确实有意义).将其与智能手机上的普通 Safari 浏览器进行比较.它会要求每个新网站的地理位置许可.应用启动时通过PhoneGap加载index.html也是一样.

This means that the geolocation call of webview (and not a native call via PhoneGap) is being triggered which will again ask for permission (which does make sense). Compare it to the normal Safari browser on your Smartphone. It'll ask for geolocation permission for every new website. It's the same when loading index.html via PhoneGap on application startup.

但是,解决方案是等待在 PhoneGap 完全加载时触发的 deviceready 事件:

However, the solution is to wait for the deviceready event which gets fired when PhoneGap has fully loaded:

document.addEventListener("deviceready", function(){
     navigator.geolocation.getCurrentPosition(onsuccess, onerror, params);
}, false);

这将使 PhoneGap API 可用,它会覆盖浏览器的默认 HTML5 地理定位调用,并通过本机调用(您已在第一个提示中接受)获取设备的地理位置.

This will make the PhoneGap API available which overwrites the default HTML5 gelocation call of the browser and get the device's geo location via a native call (which you already accepted in the first prompt).

这会起作用,因为 PhoneGap 的 API 调用与 HTML5 的标准 W3C 调用相同:http://docs.phonegap.com/en/2.2.0/cordova_geolocation_geolocation.md.html#Geolocation

This will work because PhoneGap's API calls are identical to the standard W3C call for HTML5: http://docs.phonegap.com/en/2.2.0/cordova_geolocation_geolocation.md.html#Geolocation

这篇关于如何防止 Phonegap 应用程序中的地理定位双重提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:iPhone - 从照片中获取位置信息 下一篇:在 Android 中步行/驾驶时更新位置标记并在 Google 地图上绘制路径

相关文章