Many mobile web browsers provide access to the current geo location via JavaScript (see
the W3C spec). It's very easy to use but there are a couple gotchas to be aware of. First, not all browsers support the API so you will need to take that into consideration when designing your user experience. Next, requesting the geo location from the browser will prompt the viewer to approve the request. On every page view. This is very annoying. You should store the location data away in a cookie and only periodically request updated location information. Another cool function is that the geolocation API allows your code to be notified as the location moves - perhaps your visitors take the bus or use their mobile devices while riding a bike. This is done with callbacks which is very compatible with client development and makes total sense.
Here is some sample script showing how you could use this geolocation API in your mobile or location aware web apps.
function onLocationUpdated(position)
{
// do something useful
savePosition(position);
createCookie("s_geo","on",3600);
updateLocationDisplay(position);
}
// request location
if (navigator.geolocation && !readCookie("s_geo"))
{
navigator.geolocation.getCurrentPosition(onLocationUpdated);
var watchID = navigator.geolocation.watchPosition(
onLocationUpdated, null, {
enableHighAccuracy : true,
timeout : 30000
});
}
2 comments:
So the user will be prompted once per page, but not on the updates right?
If you use JavaScript to rewrite the page rather than load a new URL, it sounds like you can get a complete web app with only a single user interruption prompting for permission, is that right?
It seemed like the browser prompted the user on every pageview which is why I used a cookie to suppress that. I don't know about using js to rewrite the page - I'm not a huge fan of those style of web apps.
Post a Comment