javascript 如何防止在Phonegap应用程序中双重提示地理定位?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13857165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to prevent double prompt for geolocation in Phonegap app?
提问by Timo
I created a PhoneGap app for iPhone that uses geolocation via JavaScript inside webview.
我为 iPhone 创建了一个 PhoneGap 应用程序,它通过 webview 中的 JavaScript 使用地理定位。
When I run the app the first time, it'll prompt me to allow geolocation for this app.
当我第一次运行该应用程序时,它会提示我允许对此应用程序进行地理定位。
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.
当我点击“确定”时,它会再次提示我同样的问题,但这次它指出“index.html”需要使用地理定位的许可。
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.
这是有道理的,因为 iOS 可能需要在第一次和第二次浏览器需要权限时允许应用程序本身的地理定位。
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)
如何防止这种双重提示?(如果可以阻止第二个提示,我就足够了)
回答by Timo
I found the cause for the issue.
我找到了问题的原因。
The call to navigator.geolocation.getCurrentPosition(onsuccess, onerror)
happens before Phonegap was fully loaded.
调用navigator.geolocation.getCurrentPosition(onsuccess, onerror)
发生在 Phonegap 完全加载之前。
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.
这意味着正在触发webview的地理定位调用(而不是通过 PhoneGap 的本地调用),这将再次请求许可(这确实有意义)。将其与智能手机上的普通 Safari 浏览器进行比较。它会为每个新网站请求地理定位许可。在应用程序启动时通过 PhoneGap 加载 index.html 时也是如此。
However, the solution is to wait for the deviceready event which gets fired when PhoneGap has fully loaded:
但是,解决方案是等待在 PhoneGap 完全加载时触发的 deviceready 事件:
document.addEventListener("deviceready", function(){
navigator.geolocation.getCurrentPosition(onsuccess, onerror, params);
}, false);
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 地理定位调用,并通过本机调用(您已在第一个提示中接受)获取设备的地理位置。
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 的 API 调用与 HTML5 的标准 W3C 调用相同:http: //docs.phonegap.com/en/2.2.0/cordova_geolocation_geolocation.md.html#Geolocation
回答by bouscher
Have a look at this: Location permission alert on iPhone with PhoneGap
看看这个: 使用 PhoneGap 在 iPhone 上的位置权限警报
The second one seems to be the Webkit alert. In order to prevent this, you seem to have to simply move all your js files to the root directory. Tell me, if it works since I'll have to address the same issue soon.
第二个似乎是 Webkit 警报。为了防止这种情况,您似乎必须简单地将所有 js 文件移动到根目录。告诉我,如果它有效,因为我很快就必须解决同样的问题。
回答by ToughPal
Finally fixed the issue.
终于解决了这个问题。
IN the index.html just move your cordova.js up
<script src="cordova.js"></script>
在 index.html 中,只需将您的 cordova.js 向上移动
<script src="cordova.js"></script>
as the first js file to be included (especially make sure it is above maps include js). This will make sure that the prompt shows only once
作为要包含的第一个 js 文件(尤其要确保它位于包含 js 的地图之上)。这将确保提示只显示一次
回答by Franz
I solved this problem by moving the
我通过移动解决了这个问题
<script src="cordova.js"></script>
as the last script to be included
作为要包含的最后一个脚本