Accessing the iPhone Location from a Webpage
The iPhone is a great device for accessing web sites while on the road. But things you look up on the road often differ from the things you'd need at home. Often the information you are looking for is directly related to your current location. This is why the new iPhone 3G comes with a builtin GPS.
Unfortunately the location data can not simply be used in a web application because Apple didn't add any possibility to do so in Mobile Safari.
But now you can access this information using a free tool from the appstore. The tool is called Search Quest. I mentioned it in my last blog post already:
There are two srceens. The first screen lets you enter a position using various options.
The second screen gives you the choice to use the position to do various things.
One (badly documented) feature of Search Quest is to use it to relay your location to any website. Search Quest provides two options to do so1):
- calling Search Quest from your web application
- calling your web application from Search Quest
Let's have a look at both options.
Option 1: Calling Search Quest
The first option is really simple: you just create a link using the pseudo protocol searchquest://
. After the protocol you put the URL of your application replacing the http://
with an uppercas H. Like this:
<a href="searchquest://Htools.splitbrain.org/gc/loc.php">Get Location</a>
Clicking this link in Mobile Safari (and having Search Quest installed of course) will quickly popup Search Quest and then redirect the user to the given site. The current position is passed as variables in the GET request.
You can try it here: http://tools.splitbrain.org/gc/loc.php
Unfortunately this method has a disadvantage: Search Quest will not update the current position but will send the data from when it last updated.
Option 2: Registering your App
The second method registers your web application in Search Quest itself. This makes it more likely the user will update the current position before accessing the web application.
To do this you need to create a special menu file on your webserver. I couldn't find a way to do this without this menu file. I also couldn't find any description of the format. But it is easy enough to figure out from an example. It basically configures a few texts and the URL(s) you want to have called with the location data.
I wrote a little example which will redirect you to the list of nearest geocaches at geocaching.com or shows a map with them. The menu file looks like this:
{ G="1"; T="Geo Caching"; B="Apps"; C=( ( { T="Please choose"; }, { T="Nearest caches"; E="The nearest caches to your location"; S="http://tools.splitbrain.org/gc/nearest.php"; }, { T="Show GC Map"; E="Display the geocaching.com map"; S="http://tools.splitbrain.org/gc/map.php"; }, { T="Show my data"; E="Display all your location data"; S="http://tools.splitbrain.org/gc/loc.php"; }, ) ); }
The URLs just do a simple redirect:
<?php header('Location: http://www.geocaching.com/seek/nearest.aspx?lat='.$_REQUEST['lat'].'&lng='.$_REQUEST['lon']); ?>
To register the menu file in Search Quest you need to enter its URL in the second screen under Access Location-Based Web Apps. Here's a series of screen shots:
After the menu URL is registered once you can always update the menu file to add or change your service URLs.
If you want to try it, register http://tools.splitbrain.org/gc/
in SearchQuest.
Summary
Search Quest gives you a simple method to get access to your user's location on their request. Personally I like the first method better as it is simpler to desribe to your users: “install Search Quest on your iPhone and click this link”.
Hopefully Search Quest will add a feature to force an update of the location data when using this method in the future.