LinkedIn Facebook Twitter bemoko blog bemoko technical pages
Tel: +44 1256 581028 Email:

Bemoko blog



iPhone 3.0 geolocation javascript API

Posted By Ian Walsh Wednesday, June 17, 2009. 32 Comments

With the iPhone 3.0 firmware released today, I thought I’d show you how to access one of the features I’ve been most looking forward to – support for the geolocation API in Safari means I can now create location aware websites.

This will be just a quick demo to retrieve and display the current longitue and latitude of your phone, along with a googlemap so we can see if the results are right.  The more interesting applications will come….

To do this we’ll use javascript and the newly added Navigator.Geolocation interface to call the getCurrentPosition() function to retrieve the current longitude and latitude of the phone, which we’ll display and pass to the googlemaps API.

The code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "">
<html xmlns="">
<title>iPhone 3.0 geolocation demo</title>
<meta content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" name="viewport"/>
function handler(location) {
var message = document.getElementById("message");
message.innerHTML ="<img src='" + location.coords.latitude + "," + location.coords.longitude + "&size=300x200&maptype=hybrid&zoom=16&key=YOURGOOGLEAPIKEY' />";
message.innerHTML+="<p>Longitude: " + location.coords.longitude + "</p>";
message.innerHTML+="<p>Latitude: " + location.coords.latitude + "</p>";
message.innerHTML+="<p>Accuracy: " + location.coords.accuracy + "</p>";
<div id="message">Location unknown</div>

The results:

Try it for yourself by pointing your Safari browser at

Update 27/7/2009: I noticed the zoom level on the google static map API wasn’t being set so the map was zoomed all the way out. I’ve added a default zoom for the demo code above. Not sure if thats a new requirement from Google’s side?

Share and Enjoy:
  • Digg
  • Sphinn
  • Facebook
  • Mixx
  • Google Bookmarks

Posted in: mobile web


  • Greg

    Jun 19th, 2009

    Can you post the demo online so people can try it?

  • ian.walsh

    Jun 21st, 2009

    I’ve posted the demo online at

  • H@nnes

    Jun 25th, 2009

    Pretty cool, did not realize until now! Now what’s missing is a way to determine which SDK is installed. Any idea? Thanks!

  • [...] The iPhone 3.0 firmware release now allows us to access the JavaScript Geolocation API [...]

  • Stanislav

    Jul 20th, 2009


    This looks like simple:
    if(typeof(navigator.geolocation) != “undefined” && typeof(navigator.geolocation.getCurrentPosition) != “undefined”) {
    /* iPhone OS3 */

  • Justin

    Jul 20th, 2009

    All that tells you is that the current user agent supports the W3C’s geolocation API ( It doesn’t tell you if the user agent is safari or iPhone 3.0. For instance, that test passes in Firefox 3.5.

  • [...] We’re already seeing a taster of this HTML5 support, with the iPhone geolocation support in iPhone 3.0. Many apps currently in the app store realm lend themselves well to a personalised microsite with [...]

  • Nelson

    Jul 21st, 2009

    Thanks for the demo code! I cribbed off it to make a little webapp that shows nearby Wikipedia articles. More details on my blog at

  • Mike

    Jul 23rd, 2009

    So with a xml document that only stores the names of cities eg: New York, Boston, London, Sydney etc. What would be the simplest way to interface the long and lat data with the city name strings.

    Does a database of city long and lats exist that i could reference with javascript?

  • Daniel

    Sep 20th, 2009

    Mike: try to use Googlemaps API -> Google knows what u want ;) they really do!

  • [...] – Determine user location. On iPhone this means limited access to the GSP/Location API. This example demonstrates how easy it is to ask Safari for the user’s [...]

  • Dylan Phillips

    Jan 7th, 2010

    Just did a head to head with my iPhoneGS versus my Droid.

    Even with position options set to {enableHighAccuracy:true, maximumAge: 0}. The I phone just stunk.

    Looks like it was giving me the Address of the cell tower. If I left Safari, and did a GetLocation from Google Mapples.

    The next calls from within Safari got the correct location. This really needs to be fixed.

    Droid rocked it out. I was able to walk through the park, updating my GeoCode every 30, through JavaScript on the browser.

  • Dylan Phillips

    Feb 22nd, 2010

    Found this entry today, definitely some changes to the result set. The problem was that my iPhone took too long to get it’s location, after a few requests. It was able to ‘lock’ in on me. In fact, during intensive tests 3 weeks ago the iPhone outperformed Droid on the GPS.

    Very happy with both platforms and the possibilities.

  • Chi Patts

    Mar 5th, 2010

    Hello, I came across this article while searching for help with JavaScript. I have recently switched browsers from Google Chrome to Firefox 3.2. After the change I seem to have a problem with loading JavaScript. Every time I go on a website that needs Javascript, the site does not load and I get a “runtime error javascript.JSException: Unknown name”. I cannot seem to find out how to fix the problem. Any help is very appreciated! Thanks

  • Asakura

    Mar 11th, 2010

    Found this entry today, definitely some changes to the result set. The problem was that my iPhone took too long to get it&#30;s location, after a few requests. It was able to 'lock' in on me. In fact, during intensive tests 3 weeks ago the iPhone outperformed Droid on the GPS.

    Very happy with both platforms and the possibilities.;

  • Craig

    Apr 21st, 2010

    Hopefully the writer is reading this comment. Any info on OS4 Beta? It seems this code does not work. I went to your test site and it returned ‘Location Unknown’

  • Ian Homer

    Apr 21st, 2010

    Thanks for the heads up. We’ll give it a go on OS4

  • Nic

    Apr 23rd, 2010

    That is beautiful.

    I have been looking for this functionality for many months now, and I have been told by a number of people (who should know better) that its not possible to get someones phone location from their browser.

    Great post


  • Ivan

    Jul 9th, 2010

    @Craig: It seems that Apple has decided to modify their support. I’ve found that the GPS data provided using the Web-based API may correspond only to the AGPS (Assisted location) which lacks accuracy. Hopefully, they will release something soon to fix or inform about how to get more accurate data using the API.

  • Umut Aydin

    Aug 10th, 2010


    I couldn’t run it on iPhone 4 Simulator. I try to show an alert box with position data. There is no error but no result also.
    Any known problem like that?

  • krycek

    Nov 20th, 2010

    You don’t have to have the newest mobile phone to use GPS and Geolocation API. Almost every mobile browser (without proxy server) can be used to read position from buidin GPS. If You have Java and GPS in Your phone – You can use mobile-gps-web-gate – see at

  • ricky

    Mar 26th, 2011

    hi ive just tried this to test, im not getting a map, but a location,

    is this example no longer valid?

  • Ian Homer

    Mar 28th, 2011

    Ricky – it’s working OK for me (just tested in Firefox 4) – what browser are you coming in one?

  • ricky

    Mar 28th, 2011

    heya, yeah it was working now, sorry i had an error in my code.

    is there anyway to assign the long and lat to a variable in php, so when using the yelp api to search bars, a list of local ones would come up based on the long and lat?

  • Joris

    Jun 24th, 2011

    I just tested this on my Iphone 4 but it’s not working, still it is working in Safari, Chrome and Firefox 5…

    Do you know why it’s not working on the Iphone 4 and how I can get this to work?


  • [...] site is BEMOKO and on the blog is a there is a post, ‘iPhone 3.0 geolocation javascript API‘, which shows exactly what I need to do in order to retrieve my current location and display [...]

  • Ben

    Dec 13th, 2012 is no longer working. Can you please update/fix.

  • Ian Homer

    Dec 14th, 2012

    Hi Ben – thanks for raising that with us and sorry, but we had a little shuffle of site and this accidentally got squashed by our blog. I’ve moved the example to so you can view it there now. Thanks – Ian

  • Terry L

    Aug 22nd, 2013

    Just as an FYI this does not work on either my ipohne 4s or my iphone 5 both with IOS 6.1

  • Terry L

    Aug 22nd, 2013

    Sorry guys, works fine, just needed to go to settings > privacy and permit safari access.

Leave a Comment

Name (required)

Email (will not be published) (required)