My Problem:
It takes two page loads from my mobile browser (testing with iphone) to get an accurate result. The first load will be half a mile or more off, and then the second load will be very close.
My question:
What needs to be done to get the accurate location on the first load?
Extra Info:
I’ve read through http://www.w3.org/TR/geolocation-API/ and am taking advantage of watchPosition with the enableHighAccuracy. Also I am using maximumAge:0 and timeout not set.
From http://www.w3.org/TR/geolocation-API/
If maximumAge is set to 0, the implementation must immediately attempt to acquire a new position object
the default value used for the timeout attribute is Infinity
My code is listed below
// Try HTML5 geolocation
if(navigator.geolocation) {
// updated this with watchPosition rather than getCurrentPosition
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var infowindow = new google.maps.InfoWindow({
map: map,
position: pos,
content: 'Location found using HTML5.'
});
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
},
// enable high accuracy
// {maximumAge:100, timeout:5000, enableHighAccuracy: true}
//
{maximumAge:0, enableHighAccuracy: true}
);
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
}
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'Error: The Geolocation service failed.';
} else {
var content = 'Error: Your browser doesn\'t support geolocation.';
}
I have the solution to getting the most accurate geolocation possible. You need to call clearWatch() after a given timeout. The timeout maybe time and or an event such as form click.
In my application I use a 5 second timeout and a click of a form element.
Here’s all the useful code snippets.
Set the onclick
Set the timeout in the function that is the parent of the watchPosition function
Create a function to listen for the form click
Create the function for calling clearnWatch()
Make sure your watchPosition can be referenced by the ID