Ok, my code is below, so I will explain what I do and what I get for the results.
I am trying to capture the location of users using navigator.geolocation.watchPosition. I have indicated both a successful and a callback. The first time I get a location, this is usually very inaccurate, so I use watchPosition to keep getting the location until it has tried 4 times or got a place with an accuracy of 500 meters.
The problems that I have are:
It NEVER enters an error callback for Android (tested on HTC Sensation and Samsung Galaxy S3). However, on the iPhone, it seems to enter error callback more than 50% of the time.
It also never expires. He just continues the search until he gets the location, etc.
- It takes from 0 to 60 seconds to get the location. Sometimes he has it before the page is loaded, at other times it takes most of a minute. There is also no distinguishable template as to why it quickly becomes fast and why not at other times.
I searched for a lot of forum posts and a lot of questions about stackoverflow, as well as specs for geolocation and any other documents. There is not much information, not to mention these issues. Do I have something wrong? Or is it 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; } } }
source share