Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geolocation doesn't stop in Phonegap

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?

like image 475
jenswirf Avatar asked Mar 16 '12 12:03

jenswirf


2 Answers

navigator._geo is the 'real' implementation I believe. I've seen recent git commits 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 commit: http://mail-archives.apache.org/mod_mbox/incubator-callback-commits/201203.mbox/%[email protected]%3E

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

UPDATE: I have some measure of success now: Edit your phonegap.js, comment 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.

like image 109
Ryan Avatar answered Sep 30 '22 04:09

Ryan


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
like image 28
Moteka Mobilee Avatar answered Sep 30 '22 05:09

Moteka Mobilee