简体   繁体   English

PhoneGap上的geolocation.getCurrentPosition

[英]geolocation.getCurrentPosition on PhoneGap

I made an app using PhoneGap that needs to access the user's current latitude and longitude to function. 我使用PhoneGap开发了一个应用,该应用需要访问用户当前的经度和纬度才能正常运行。 Here's what I did: 这是我所做的:

navigator.geolocation.getCurrentPosition(function(pos) {
    // code using pos
});

On the browser this asks your permission to access the info, but on the finished app it doesn't ask anything and just doesn't work. 在浏览器上,这会询问您是否允许访问信息,但是在完成的应用程序上,它不会询问任何问题,只是无法正常工作。 Is there anything I'm doing wrong/missing? 我在做错什么/错过吗?

@Dogmatics, there is not enough information to debug your problem. @Dogmatics,没有足够的信息来调试您的问题。 Assuming you turned on the GPS and Location services, then you forgot to wait for the 'deviceready' event. 假设您打开了GPS和定位服务,则您忘记等待“ deviceready”事件。

From the documentation : 文档中

This is a very important event that every Cordova application should use. 这是每个Cordova应用程序都应使用的非常重要的事件。

Cordova consists of two code bases: native and JavaScript. Cordova由两个代码库组成:本机代码和JavaScript。 While the native code is loading, a custom loading image is displayed. 加载本机代码时,将显示自定义加载图像。 However, JavaScript is only loaded once the DOM loads. 但是,仅在DOM加载后才加载JavaScript。 This means your web application could, potentially, call a Cordova JavaScript function before it is loaded. 这意味着您的Web应用程序可能会在加载之前调用Cordova JavaScript函数。

The Cordova deviceready event fires once Cordova has fully loaded. 一旦Cordova完全加载,就会触发Cordova deviceready事件。 After the device has fired, you can safely make calls to Cordova function. 触发设备后,您可以安全地调用Cordova函数。

If this proves to be correct, you will want to read #4 in: 如果证明正确,那么您将需要阅读以下内容中的#4:
Top Mistakes by Developers new to Cordova/Phonegap Cordova / Phonegap新手开发人员的主要错误
https://github.com/jessemonroy650/top-phonegap-mistakes/blob/master/new-to-Phonegap.md https://github.com/jessemonroy650/top-phonegap-mistakes/blob/master/new-to-Phonegap.md

Best of Luck 祝你好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM