getCurrentLocation strange loop?

357 views
Skip to first unread message

Knaps

unread,
Jul 16, 2011, 7:05:46 AM7/16/11
to phonegap
Hi there,

I'm having an issue with geolocation and getCurrentLocation()
function. Please find attached code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/
TR/html4/strict.dtd">
<html>
<head>
<meta name="viewport" content="width=device-width, initial-
scale=1.0, maximum-scale=1.0, user-scalable=no;" />

<meta http-equiv="Content-type" content="text/html;
charset=utf-8">

<script type="text/javascript" charset="utf-8" src="phonegap.
0.9.5.1.min.js"></script>
<script type="text/javascript" charset="utf-8">

function onBodyLoad()
{
document.addEventListener("deviceready",onDeviceReady,false);
}

function onDeviceReady()
{
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}

function onError(error)
{
alert('code: ' + error.code + '\n' + 'message: ' + error.message +
'\n');
}

function onSuccess(position)
{
alert('Latitude: ' + position.coords.latitude +
'\n' +
'Longitude: ' + position.coords.longitude + '\n'
+
'Altitude: ' + position.coords.altitude + '\n'
+
'Accuracy: ' + position.coords.accuracy + '\n'
+
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n'
+
'Heading: ' + position.coords.heading + '\n'
+
'Speed: ' + position.coords.speed + '\n'
+
'Timestamp: ' + new Date(position.timestamp) +
'\n');
}

</script>
</head>
<body onload="onBodyLoad()">
<h1>Geolocation Test</h1>
<p>getCurrentPosition() seems to go into infinite loop...
</body>
</html>

What happens is, I run the application, the alert box pops up with all
information, but I don't even manage to tap "OK" and the new alert box
pops up with the same info and so on. I managed to stop the loop few
times but only after I tapped "OK" button like 20 times...

This is quite problematic as in the code I'm developing I rely on
success callback function to send some data to the server, but I want
it to happen only once. Because of what's happening with
getCurrentLocation(), I'm actually flooding server with number of
requests (since getCurrentLocation() asynchronous I needed to put cal
to the function sending requests to the server in the success
callback).

I would've thought that getCurrentLocation() should return current
location just once and maybe even disable GPS after that. For other
cases watchLocation() could have been used.

I'm encountering this problem on iPhone 3GS iOS 4.3.3 and PhoneGap
0.9.5.1.

Any help would be greatly appreciated.

Thanks,
Michael

MIPs

unread,
Jul 18, 2011, 6:27:35 AM7/18/11
to phon...@googlegroups.com
I am using .getCurrentPosition() quite happily and did so in Phonegap 0.9.5.1, although I have now moved on to 0.9.6.
 
I can't see anything immedialtely wrong in your code, but I don't have time right now to run a test (will try to do so later).
 
Thoughts that occur, are:
 
1.

MIPs

unread,
Jul 18, 2011, 6:30:45 AM7/18/11
to phon...@googlegroups.com
Opps - I think I hit post by mistake
 
Thought that occur are:
 
1. Is it DOM initialiasation issue? - what if you start the code on a button/link? Although you have correctly waited for deviceready.
2. Try replacing Alert with a console.log - does that make any difference?
 
GPS will remain on once a call has been made to location services.  To turn it off use: "navigator.geolocation.stop();"
 

tcotav

unread,
Jul 28, 2011, 12:24:41 AM7/28/11
to phonegap
Sorry for resurrecting this but I saw it while looking for something
else. My understanding of this (and my own experience with that
looping) was that the navigator.geolocation continues to try to get a
more accurate reading. So, your alert shows you all of those
attempts. Here's how I worked around that (minus a bunch of other
custom stuff so I might've biffed the code, but you'll get the gist).
**Please someone correct me if ANY of what I've got below is
incorrect.**

------------------------
var fGotPosition=false; // flag to toggle when we get a GOOD ENOUGH
coordinate set
var MIN_ACCURACY=1000; // minimum accuracy we'll accept in meters
var GEO_OPTS_DICT={enableHighAccuracy:true, maximumAge:120000, timeout:
120000, frequency:60000};
var geoLocAccFailCount=0;

/*
just the function called to "do SOMETHING" with coords
*/
function publishLocation(location) {
// do stuff here with location
console.log(location.timestamp + ":"
+ location.coords.latitude + ":"
+ location.coords.longitude + ":"
+ location.coords.accuracy;
return;
}

function onGeoLocSuccess(location) {
/* if we have a location object that fits our accuracy criteria
then there is no need to continue processing
*/
if(fGotPosition) { return; }

// check accuracy of coords that we got
if (location.coords.accuracy <= MIN_ACCURACY) {
//
// might consider checking the timestamp here too to make sure
we've got something fresh
// location.timestamp?
//
console.log("aaaand we're in... got location");

// this is flag telling us we GOT the data and ignore the rest
fGotPosition = true;

// do something with the value -- whatever that ends up being
publishLocation(location);

// tell the geolocation object to stop geolocating
navigator.geolocation.stop();
}
else {
// I put this in just to see how often we fail count -- remove in
prod
geoLocAccFailCount++;
console.log("geolocsuccess -- not accurate enough. count: "
+ geoLocAccFailCount
+ " accuracy was: " + location.coords.accuracy);
}
}

/*
this is the method invoking the geolocation.
*/
function geoLocate() {
fGotPosition=false; // make sure we set this off before we start
in case we repeatedly invoke geoLocate()
geoLocAccFailCount = 0; // again, just for fun really -- not
necessary

// and of course -- this
navigator.geolocation.getCurrentPosition(onGeoLocSuccess,
onGeoLocError,
GEO_OPTS_DICT);
}






On Jul 16, 4:05 am, Knaps <knap...@gmail.com> wrote:
> Hi there,
>
> I'm having an issue withgeolocationand getCurrentLocation()
>           <h1>GeolocationTest</h1>
>           <p>getCurrentPosition() seems to go into infinite loop...
>   </body>
> </html>
>
> What happens is, I run the application, the alert box pops up with all
> information, but I don't even manage to tap "OK" and the new alert box
> pops up with the same info and so on. I managed to stop the loop few
> times but only after I tapped "OK" button like 20 times...
>
> This is quite problematic as in the code I'm developing I rely on
> success callback function to send some data to the server, but I want
> it to happen only once. Because of what's happening with
> getCurrentLocation(), I'm actually flooding server with number of
> requests (since getCurrentLocation() asynchronous I needed to put cal
> to the function sending requests to the server in the success
> callback).
>
> I would've thought that getCurrentLocation() should return current
> location just once and maybe even disable GPS after that. For other
> cases watchLocation() could have been used.
>
> I'm encountering this problem oniPhone3GS iOS 4.3.3 and PhoneGap
Reply all
Reply to author
Forward
0 new messages