Daniel Fone

Ruby/Rails Engineer

Browser Geolocation API Demo

I was recently investigating the feasibility and capability of browser-based Geolocation, especially on mobile devices as an alternative to native mobile development. The Geolocation API is a W3C standard that defines a high-level JavaScript API for websites to query the physical position of a device. According to the ever-useful MDN docs, the APIs are well-supported.

There are two ways to retrieve location:

Both methods can be configured with a timeout, a maximum age (for returning cached data), and an accuracy hint. Returned positions have a timestamp and coordinates, which includes latitude/longitude with 95% confidence range, altitude with 95% confidence range if supported, and speed and heading if supported. Any request for location data will prompt the user for permission.

Testing across a range of desktop and mobile devices, the behaviour varied somewhat. In particular, the interactions of the location cache with the timeout and maximumAge options was hard to understand, and maximumAge wasn’t always strictly followed.

I’ve put together single-page HTML demo, which is <200 LOC. I’ve included an iframe version below.

Notes & Questions

Further Reading

comments powered by Disqus