The W3C Geolocation API on iPhone with Locatable
I've ported the W3C's draft Geolocation API so it can be used from an iPhone with Locatable installed (my Javascript skills are far from elite, but with enough prodding and old-school alert() debugging, I got there). This means that in addition to the redirect API (which is nice for embedding static links to pages that can take lat/long coordinates), you can now get at location information on demand via Javascript, through what is likely to become the standard API in future browsers.
To use it, just include this in your HEAD:
You can download and install the script locally if you like (but please check back for new versions from time to time).
Then, to use it, just use the standard W3C-prescribed approach via a global object called
There's a test page up at http://www.tralfamadore.com/test-w3c.html that demonstrates this functionality.
Some implementation notes:
Update (31 Aug 08): Upon some reflection, I decided it's best not to try to automatically install as the
To use it, just include this in your HEAD:
<script type="text/javascript"
src="http://lbs.tralfamadore.com/w3c-api.js">
</script>
You can download and install the script locally if you like (but please check back for new versions from time to time).
Then, to use it, just use the standard W3C-prescribed approach via a global object called
Locatable, e.g.// Callback handlerThe only addition to the W3C API is an
function gotLocation(position) {
alert('You are at (' + position.latitude + ','
+ position.longitude + ').');
}
// Use this anywhere you like
Locatable.getCurrentPosition(gotLocation);
isEnabled() method. This will attempt to figure out if the API will work on the current browser. Right now this merely checks if someone is on an iPhoneOS device, but might be more sophisticated in the future.There's a test page up at http://www.tralfamadore.com/test-w3c.html that demonstrates this functionality.
Some implementation notes:
- The same logic applies when sharing location as with the redirector. Depending on user preferences, an alert will ask them to confirm if they want to share their location. If they decline, you'll get an error callback if you provide the second argument to getCurrentPosition.
- If the location needs to be refreshed, the app will launch, update the reading, and then return to Safari. This can take some time (in Locatable 0.3, up to 20 seconds, depending on the user accuracy setting). On jailbroken phones with default Locatable settings this is unlikely to occur as the daemon will be updating location in the background, but an AppStore version will not have this advantage, so be mindful of this.
- W3C PositionOptions (the accuracy hint) does nothing at the moment.
- watchPosition() is "implemented" (that is, the function exists), but you'll only ever get one reading, so it's not entirely useful.
- The accuracy reading in the position object is currently the user-set minimum accuracy level (a round number like 10, 100, or 1000 meters), not the device-reported accuracy of the reading itself. This is likely to change in future versions.
- Altitude and velocity are not implemented yet and yield null values.
- w3c-api.js will attempt to detect if you're running an iPhone or iPod Touch and not install itself otherwise. It also won't overwrite navigator.geolocation if it's already implemented (non-null).
- It probably goes without saying, but you should include the w3c-api.js script on every page you want to use it in.
Update (31 Aug 08): Upon some reflection, I decided it's best not to try to automatically install as the
navigator.geolocation global, so the script has been updated to use a global called Locatable (capital L) instead. You're free to assign it to navigator yourself (i.e. navigator.geolocation = Locatable). Also added the isEnabled() method.Labels: api, geolocation, iphone, javascript, locatable, w3c

2 Comments:
I'd like just to ask and expand on your article, as I was confused by it and failed to get it to work. I hope by asking these questions others may also benefit.
1) The way I know understand it is when the application is running on your phone, it lets your site know not where the phone is, but that is available to give location data. Is that correct?
2) The webpage script as above, the script linked is the Javascript API for geolocation. The second part of the script goes on the page. You have set it up so when the button is pressed it calls the API. The API then tries to see if the phone is available for location and tries to get the information from it on the current posistion.
3) How does the API call know which phone it is trying to locate? This is confusing to me as each phone must have an unique ID to allow you to get the corrects phones location data?!
4) I have set up my phone with Locatable and added the script to a webpage. When the button is clicked the script runs (using FF so I can see it contacting your site) and always shows "no location available". Which is the same responce we get on your test page.
What is required to make the location available and secondly what process can we use to debug why the location might not be available?
I hope that all makes sense and I hope you will be able to assist me and no doubt others with this.
Hi Charlie, let me try to answer these.
1) The idea is that if you are on your iPhone and browse to a web site, that web site can (via javascript) get access to your location. Currently, this is a function that is available to iPhone applications, but can't be done in the browser without having Locatable installed.
When the app is installed, it saves your location in a local database that can be read via Javascript in Safari. There is no server database of user locations. For that you want something like Yahoo's FireEagle, and a client on the iPhone that lets the user update their location record on the FireEagle server.
2) That's right, but...
3) The only phone that can be located is the one that is making the request. (i.e. the user on their iPhone is in Safari and has browsed to your page)
4) This doesn't work in FireFox (or any PC browser for that matter) at the moment, only Mobile Safari.
Post a Comment
<< Home