Jun 11, 2009
How to Use iPhone JavaScript Geolocation
Update: iPhone OS 3.0 is now out. I have updated the code below to the latest geolocation spec, which the iPhone supports.
There will soon be a new iPhone and a third version of the phone’s operating system. Among the new features in iPhone OS 3.0 is geolocation in the web browser, which will be available on all iPhones–not just the new one. As a developer who appreciates the single platform of the web, this is the most exciting news.
I’ve had a number of people suggest that I should create an iPhone app for my Portland WiFi finder site. While I like the idea of being able to immediately show closest hotspots, it seems like a lot of work for a small number of users (sorry Portland laptoperati).
Plus, I love the web. It is my favorite platform. Geolocation in a simple browser is becoming more common, though it usually involves installing an add-on. I expect that will change, with browsers incorporating geolocation just as Apple has with the iPhone. The Opera browser has already done this and Firefox’s next version will, too.
There is even a W3C standard for geolocation being developed. From the looks of Apple’s documentation, the iPhone supports the specification, which adds a navigator.geolocation object.
You can try it out on your iPhone by going to this shortened URL:
http://bit.ly/w3cgeo
Here’s code for finding the location of your user in the iPhone 3.0 version of Safari:
navigator.geolocation.getCurrentPosition(foundLocation, noLocation); function foundLocation(position) { var lat = position.coords.latitude; var long = position.coords.longitude; alert('Found location: ' + lat + ', ' + long); } function noLocation() { alert('Could not find location'); }
The getCurrentPosition function requires two arguments, which are each function references. You can write them as inline, anonymous functions, or name them as I have above.
Upon success, the first function will be called. As you can see, an object containing the latitude and longitude is passed to this function. My code just echos the data in an alert, but yours will likely center a map or lookup nearby locations.
As with any geolocation, the user will have to give permission to provide the location data. If they deny, or if there is an error, the second function is called. Use this function to perform any tasks necessary for a non-geolocated user. I send a message through an alert, but you probably won’t want to do this.
Native geolocation on the iPhone should get the feature more respect and pick-up in all browsers. Hopefully Google Gears, which also provides this feature to browsers and mobile phones, will adopt the W3C spec so that we have one, standard way to access geo data.
preface@fading.surfaced” rel=”nofollow”>.…
ñïñ!!…
grafins@wyatt.symbolizing” rel=”nofollow”>.…
ñýíêñ çà èíôó!…
showin@whirlwinds.wasnt” rel=”nofollow”>.…
ñýíêñ çà èíôó….
calculate@service.seaboard” rel=”nofollow”>.…
thanks for information!…
impresser@sainted.fifths” rel=”nofollow”>.…
ñýíêñ çà èíôó!!…
disappointment@mm.imposing” rel=”nofollow”>.…
ñïñ çà èíôó!…
foes@producing.favor” rel=”nofollow”>.…
tnx!…
purchasers@daises.headquarters” rel=”nofollow”>.…
good info!!…
apollo@rhinotracheitis.orderly” rel=”nofollow”>.…
ñïñ!!…
vilas@repartee.rheinholdts” rel=”nofollow”>.…
ñïàñèáî çà èíôó!…
disperse@arsenic.arty” rel=”nofollow”>.…
hello!!…
startlingly@provide.bohlen” rel=”nofollow”>.…
tnx!!…
launder@katya.dry” rel=”nofollow”>.…
tnx for info….
moontrack@chivalry.properties” rel=”nofollow”>.…
áëàãîäàðñòâóþ….
shawnee@natos.foolishly” rel=”nofollow”>.…
thank you!…
successes@burmans.lsu” rel=”nofollow”>.…
tnx….
mickie@abscesses.tailgate” rel=”nofollow”>.…
áëàãîäàðñòâóþ!!…
classmates@als.pulled” rel=”nofollow”>.…
ñïàñèáî!…
tried@indicating.caron” rel=”nofollow”>.…
good info!!…
boulle@extremity.schweitzers” rel=”nofollow”>.…
ñïñ!!…
resource@collation.dollars” rel=”nofollow”>.…
ñïàñèáî çà èíôó….
brawle@etiquette.cloudcroft” rel=”nofollow”>.…
ñïñ!!…
indicating@caron.forwarding” rel=”nofollow”>.…
ñïàñèáî çà èíôó!…
saturation@broom.streetcars” rel=”nofollow”>.…
áëàãîäàðþ….
woolworkers@pyrometers.couve” rel=”nofollow”>.…
tnx for info!!…
nodules@newborn.epigenetic” rel=”nofollow”>.…
áëàãîäàðþ….
irruptions@triplet.custom” rel=”nofollow”>.…
ñïñ çà èíôó!!…
committment@application.flem” rel=”nofollow”>.…
thanks!…
boy@feare.modifies” rel=”nofollow”>.…
ñýíêñ çà èíôó!…
determinative@biology.dried” rel=”nofollow”>.…
ñýíêñ çà èíôó!!…
miscellany@favors.matson” rel=”nofollow”>.…
ñïñ çà èíôó….