tralfamadore.com
get unstuck, in time

28 August 2008

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:
<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 handler
function gotLocation(position) {
alert('You are at (' + position.latitude + ','
 + position.longitude + ').');
}

// Use this anywhere you like
Locatable.getCurrentPosition(gotLocation);
The only addition to the W3C API is an 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.
I'd consider this a beta version — I've done some basic testing but haven't tried too many use cases. Let me know how it works for you in the comments. 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: , , , , ,

11 Comments:

Anonymous Charlie said...

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.

November 6, 2008 1:58 PM  
Blogger Wesley said...

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.

November 7, 2008 11:31 PM  
Blogger biyectivo said...

Hi,
First of all congratulations for this great app and code.

Secondly, I understand there is currently no way (in iPhone or any other device) to access location information from a WAP or XHTML page, using scripting, WITHOUT installing any applications. Is this true?

Thirdly, can this app also track the position through triangulation in case GPS data is not available?

Fourthly, is it possible to deploy Locatable by a WAP page (analogous to an activex installation trigger or java applet)? This would enable users to log in to the page and automatically get Locatable installed without going through the hassle of acquiring it through Cydia and/or AppStore.

Lastly, I would like to review whether this can be used on a commercial application. If you can please message me privately at biyectivo at gee mail dot com.

Thank you very much and again congratulations for this great app.

January 19, 2009 9:54 PM  
Blogger Wesley said...

@biyectivo:

1) Thanks!

2) That's true, as far as I know. It's certainly possible that Apple could choose to implement this natively in the browser in the future (if they do so I hope they will use a similar approach).

3) Yes -- Locatable uses the iPhone SDK's CoreLocation framework, which is a generic interface that uses GPS, cell ID location/triangulation, or WiFi access point location lookup as appropriate. That means it works even on an iPod Touch in most instances.

4) Yes, sort of. It's possible to push the user into the download page for Locatable in the AppStore (once that exists), and my plan is to provide an optional argument to the API, something like "promptForInstall". If that option is set to true and the user doesn't have the app installed, they'll be sent to the AppStore to download it. Unfortunately, they will have to manually re-open Safari once that's done.

I'll follow up on your commercial app question, but in general, the intent is that this is completely open for commercial and non-commercial applications and sites.

January 20, 2009 12:00 AM  
Anonymous Daniel said...

Hello there,

I have a problem, currently, with the app. It keeps asking for my permission, whatever options i select in the settings, and i also tried reinstalling.

It does it on both my site as the featured sites.

I'm using an iPod touch and 0.3-1.

Any solution for this? Or am i missing something?

January 30, 2009 10:52 PM  
Blogger Wesley said...

Daniel-- this is a known (but little encountered) bug in the current version. If you first visit a site while "ask always" is turned on, you can never get it to stop asking. The next version will give you the ability to change settings per site.

To kill it, you need to delete Safari's database... go to Settings app --> Safari --> Databases. You should see a database called "Locatable". Tap Edit and Delete it. That should zap all your settings. I don't think you should have to reinstall after doing that, but try it if you get any additional errors.

January 31, 2009 1:53 AM  
Anonymous Daniël said...

Hello Wesley,

Thank your for your comment. I tried removing the database, and reinstalled the program - it was needed.

But i still get every page refresh the question, and since the reïnstall, i never used 'ask always'.

Is it maybe an idea to restore my whole iPod? Since the config stays saved after removing the program.

Daniël

January 31, 2009 3:24 PM  
Blogger Wesley said...

Daniel, if you email me directly (wes@tralfamadore.com) I'll help you with this. Can you tell me what site you're using that is causing the issue? Maybe there's something strange about the domain name that is confusing the script.

January 31, 2009 3:40 PM  
Blogger Mike said...

Hi Wesley,

Just started playing with your app. The test script on you site works fine. However, when I try to run it locally on my server I get hit with a 404 error /robots.txt not found. Seems kinda odd. I added a default robots.txt file and sill get the error message. What needs to be on a server to get it running??

February 9, 2009 12:21 AM  
Blogger Tuup said...

Im also getting a 404 error when trying to run the script, tried exactly your testpage locally and on a remote server but both gave the same result, it seems to try to get to the page the script is but it refers to the page via this http thing "http%3A%2F%2F" instead of http:// dont know if that is the problem tried to fix it but dind't find a way to. Am i doing something wrong??

In case you want to check the page i used here it is.
When it says "Click here to enter website that means it got a 404 error.

March 26, 2009 1:47 PM  
Blogger Seth said...

I know that the original intent of this app has been encorperated into the iphone firmware, but I would like to use it to send my location to a server. I am having problems getting relocatable to spit out anything though. Can anyone confirm for me if this still works for iphone 3.01?

September 5, 2009 5:58 PM  

Post a Comment

<< Home

Copyright (C) 2001-2008 Tralfamadore.com