I'm experimenting with HTML5 geolocation, and embedded a small test script into a page to return my present co-ordinates.
The current application I have in mind for using this is as a 'nice to have' feature on site i'm working on - it includes a 'find my nearest' lookup on some locations, and I figured that if you had a location aware device, I could easily include 'to my current location' alongside the normal 'to my postal/zip code'. I'm not interested in loading a bunch of extra libraries and fallbacks for such a small and non-essential feature. If you have a capable device, great, if not, you won't ever see the option.
So I tried the script on an iPad, and as expected - I was prompted for permission to use my present location, to which I agreed, and my test script returned my present location. Total win.
I tried the same on my desktop, since i'm using Firefox 4 and its a HTML5 compliant browser. It asked me if I wanted to share my location, and then promptly returned the error that it didn't know my location (because its a desktop computer and has no GPS). I thought this rendered the original question of 'do you want to share your location' somewhat pointless - it could needlessly annoy people who might have thought they could use a feature that they in fact can't.
So, what is a reliable technique to detect if:
a) The browser can access HTML5 geolocation
AND
b) The browser knows or can find out what the users location is.
Without actually calling the geolocation function beforehand, and asking the user an annoyi开发者_运维百科ng, and unnecessary question?
For a) i'm simply using:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showCoords,handleGeoErrors);
}
But for b) the only answer I have involves having called getCurrentPosition, which triggers the question to the user.
Any thoughts on this, anyone?
There's not going to be a way to do this because the way a browser finds out whether or not it can get geolocation information (in the absence of a direct connection to a GPS device) is to send a request to the configured geolocation service and see what it says. Unfortunately at that point it's already shared the personal information that the annoying question is supposed to prevent being shared without permission.
精彩评论