最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Geolocation doesn't stop in Phonegap - Stack Overflow

programmeradmin1浏览0评论

When I run

window.onload = function () {
   document.addEventListener("deviceready", getGeolocation);
}

function getGeolocation() {
 navigator.geolocation.getCurrentPosition( successCallback, errorCallback, {maximumAge: 0});
}

or

function getGeolocation() {
 watchGeoMarkerProcess = navigator.geolocation.watchPosition(updateCallback, errorCallback);
}

and then

function updateCallback(position) {
   if (position.coords.accuracy < 100) {
            navigator.geolocation.clearWatch(watchGeoMarkerProcess);
   }
}

in my app on iOS 5 using phonegap it seems to get stuck, since the geolocation indicator-icon stays in the top bar and it never goes away, which I take to mean that the GPS doesn' get turned off. Also, sometimes I don't get any coords at all, throwing a time-out error.

I don't think there is anything wrong with the code since it works just fine as a webapp.

Any ideas?

When I run

window.onload = function () {
   document.addEventListener("deviceready", getGeolocation);
}

function getGeolocation() {
 navigator.geolocation.getCurrentPosition( successCallback, errorCallback, {maximumAge: 0});
}

or

function getGeolocation() {
 watchGeoMarkerProcess = navigator.geolocation.watchPosition(updateCallback, errorCallback);
}

and then

function updateCallback(position) {
   if (position.coords.accuracy < 100) {
            navigator.geolocation.clearWatch(watchGeoMarkerProcess);
   }
}

in my app on iOS 5 using phonegap it seems to get stuck, since the geolocation indicator-icon stays in the top bar and it never goes away, which I take to mean that the GPS doesn' get turned off. Also, sometimes I don't get any coords at all, throwing a time-out error.

I don't think there is anything wrong with the code since it works just fine as a webapp.

Any ideas?

Share Improve this question edited Mar 23, 2012 at 11:04 Titouan de Bailleul 12.9k11 gold badges69 silver badges122 bronze badges asked Mar 16, 2012 at 12:39 jenswirfjenswirf 7,33711 gold badges51 silver badges68 bronze badges 1
  • FYI, this appears to be a non-issue in latest Cordova (ftw). Works fine for me now using ondeviceready and watchPosition + clearWatch. – jenswirf Commented Mar 30, 2012 at 9:45
Add a ment  | 

3 Answers 3

Reset to default 3

navigator._geo is the 'real' implementation I believe. I've seen recent git mits where they are trying to over-ride navigator.geolocation but apparently failing on iOS. Looking at the phonegap source code gave me the idea to try the real call instead.

Here is the git mit: http://mail-archives.apache/mod_mbox/incubator-callback-mits/201203.mbox/%[email protected]%3E

Here is another thread on the problem: https://groups.google./forum/?fromgroups#!topic/phonegap/W32yYpV28W8

UPDATE: I have some measure of success now: Edit your phonegap.js, ment out lines 3451-3453 which look like this: __proxyObj(navigator.geolocation, navigator._geo,...

You will get an ugly permission alert.. but the location should work. The reasoning behind this change is that you will now use safari's location detection, not PhoneGaps.

UPDATE2: ..and the problem with PhoneGap turned out to be a conflict with another javascript library, in this case dragdealer.js. So double check for any suspicious variable names like "Location" or "Position" in any other javascript you are using. For some reason this conflict was not a problem on platforms other than iOS.

For what it's worth I have a same problem and these fixes did not work. But they may for you:

  • make sure you get the location after onDeviceReady() has been called
  • try using navigator._geo.getCurrentPosition

I had the same problem, although on Android.

Adding the enableHighAccuracy option caused it to start working:

navigator.geolocation.getCurrentPosition(onSuccess, onError, {enableHighAccuracy:true} );
发布评论

评论列表(0)

  1. 暂无评论