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);
> 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