Okay, my code is below so I'll explain what I'm doing and what I'm getting for results.
I'm trying to grab the users location using navigator.geolocation.watchPosition. I've specified both a success and error callback. The first time I get the location it's usually very inaccurate so I use watchPosition to keep getting the location until it's either tried 4 times or got a location that is accurate to within 500 metres.
Problems I am having:
It NEVER enters the error callback for android (tested on HTC Sensation and Samsung Galaxy S3). On the iPhone however, it seems to enter the error callback over 50% of the time.
It also never timesout. It just keeps searching until it's received a location.
I've searched many a forum post and many a stackoverflow question as well as the spec for geolocation and any other docs out there. There's not very much information on it, let alone these problems. Is there any I'm doing wrong? Or is this just the nature of working with location?
if(navigator.geolocation){
var geo_count = 0;
var wpid = navigator.geolocation.watchPosition(success, error, {
enableHighAccuracy: true,
maximumAge: 0,
timeout: 10000
});
function success(position){
alert(position.coords.accuracy)
geo_count = geo_count + 1;
if(position.coords.latitude && position.coords.longitude && position.coords.accuracy < 500){
navigator.geolocation.clearWatch(wpid);
alert('position obtained!');
}
else if(geo_count > 4){
navigator.geolocation.clearWatch(wpid);
alert('inaccurate position obtained');
}
}
function error(error){
switch(error.code){
case 1:
alert('permission denied');
break;
case 2:
alert('position unavailable');
break;
case 3:
alert('timeout');
break;
default:
alert('unknown error');
break;
}
}
}
If you are facing problems with geolocation on your website running our WordPress Directory theme using Google Chrome browser, it's because of Google's decision to allow geolocation functionality only for websites that use SSL certificates.
The Geolocation method watchPosition() method is used to register a handler function that will be called automatically each time the position of the device changes. You can also, optionally, specify an error handling callback function.
The watchPosition method returns a unique transaction ID (number) associated with the asynchronous call. Use this ID to cancel the watchPosition call and to stop receiving location updates.
I recommend to check using:
if (typeof navigator.geolocation === 'object' && typeof navigator.geolocation.watchPosition === 'function') {
...
}
"The Geolocation.watchPosition() method is used to register a handler function that will be called automatically each time the position of the device changes. You can also, optionally, specify an error handling callback function."
Maby it doesn't trigger because the position is still the same? ^^
Try to debug it using Geolocation.getCurrentPosition() with an Interval or Webworker: https://developer.mozilla.org/en-US/docs/Web/API/Geolocation
Good luck
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With