HTML and CSS Reference
In-Depth Information
The alternative error: on success
When I once visited a page that was supposed to detect my location,
whilst working from home in Brighton on the south coast of England,
the map placed me dead in the centre of London. I checked under
the hood using browser's web console and could see the accuracy
of the geolocation request was set to 140,000 meters—that's about
90 miles of inaccuracy; as a radius that's pretty damn inaccurate! It's
understandable how the site wasn't sure exactly where I was. I would
strongly recommend that while developing applications that use
geolocation you also check the accuracy of the success call. If the
accuracy is set to such a large value, it might be worth ignoring the
data altogether, treating it the same as an error, and providing your
normal fallback mechanisms—such as asking the user to enter his
location manually. However, the accuracy is all about context. If your
application was helping me to find the closest hospital, I'd expect it to
be accurate to about city size. If your application was offering a county
view of the current weather system, it would be fine if the accuracy
was to 90 miles—as it still puts me in England.
Configuring the geolocation
Finally, the third argument to both getCurrentPosition and
watchPosition contains the geolocation options. All the geoloca-
tion options are optional, as you've seen, and are constructed
as follows:
enableHighAccuracy (Boolean, default false)
timeout (in milliseconds, default infinity [represented by 0])
maximumAge (in milliseconds, default 0)
For example, to request high accuracy and a two-second time-
out, and to never use old geo data, call getCurrentPosition
using the following options (where success and error are pre-
defined functions):
navigator.geolocation.getCurrentPosition(success, error, {
enableHighAccuracy: true,
timeout: 2000,
maximumAge: 0
We already encountered enableHighAccuracy —it tells the device
to try to get a more accurate reading on the latitude and longi-
tude— timeout tells the geolocation lookup how long it should
Search WWH ::

Custom Search