background image

Location, Location, Location

jonarne's picture
Posted by jonarne - 07 Jun 2009
Twitter share icon Facebook share icon Google Plus share icon

Location based services (LBS) has been a buzz word and a hype since birth of mobile services. I'm not saying that it is a failure! Many great services have been using LBS with relatively great success. When I say "relatively", it is because we until recently have been quite dependent on what the mobile operators are offering in terms of access to their systems. Not all operators offer such services, the service quality has been to poor, data accuracy not good enough etc. And maybe most important; getting the location is cool, but not always really adding value for the user. I remember an early WAP site in Norway offering the service of finding the nearest ATM from your current position. Technically and theoretically it worked well, but in real life, useless. The wbmp (black and whit bit map image) map pin pointing the ATM made my LCD screen look broken, the system was unable to tell me exactly where I was on the map (at best within a 300 meter radius), so no route description. Easier to ask for directions...

Today, fortunately things are changing. We have GPS on phones and we have lots of geo-tagged content. Google and Yahoo provide extensive APIs, Flickr, Panoramio, LastFM, YouTube to mention a few, are all providing location as metadata to content items. Its even quite common for newspapers to tag news articles with a longitude and latitude.

So, as a believer in the web as a platform for application development, I have done some simple tests and demos on location I hope some of you find interesting.

We have at least 5 ways of getting the location of the end user surfing a mobile site:

  1. Make the browser talk to the GPS
  2. Use some kind of 3rd party tools
  3. Do a location lookup on IP
  4. Geo coding of user input
  5. Ask the mobile operator for user location

I have picked a few alternatives to illustrate the above. There are many more APIs and providers out there (feel free to share your favourites!).

Compared to applications you install on your phone, fte biggest obstacle of creating location aware services running in the browser is that you can't get access to the GPS. This is changing. W3C are specifying a method where the application running in the browser can ask the browser for the users current location. This is done via a javascript API which gets the current location from the GPS or other sources. I guess there will be a fall back chain with IP lookup or even user input, so you're not dependent on the device having GPS. Rumors say that this geolocation API will be included in the iPhone 3.0 OS. It is really simple; here is the javascript code:

  function(position) {  
    alert('Current lat/lon is: ' + position.coords.latitude + ',' + position.coords.longitude);
      alert('Sorry, could not find you... error code: '+error.code);

I have also made an example for mobile here. You will need a phone with javascript support to get the most out of the demo, but a desktop web browser will do as well. If you like to test w3c geolocation in real life, Download the beta Firefox or Opera.

Google Gears has a similar solution. Currently, gears will work only in Windows/IE and Androids, but still very cool. The javascript code looks a tad different. After initializing gears with this script you can access location like this:

var geo = google.gears.factory.create('beta.geolocation');
  function(position) {
    alert( + ', ''. Current lat/lon is: ' + position.latitude + ',' + position.longitude);
  function (positionError) {
    alert('Attempt to get location failed: ' + positionError.message); },
    { enableHighAccuracy: true, gearsRequestAddress: true }
Try it.

Google also have a javascript library called "ClientLocation".  This library tries to locate your IP address. In a mobile setting this is not ideal, because you are probably connected via your mobile operators gateway, and the gateway will be localized, not you. On wireless spots, your home LAN however, it will probably work good enough.
Include this line of code in your <head> element:

<script type="text/javascrip"; src=""></script>

And access the location data like this:

alert('Current lat/lon is: ' + google.loader.ClientLocation.latitude&nbsp; + ',' + google.loader.ClientLocation.longitude);

Demo of ClientLocation.

Other 3rd party tools to try are for example Loki and Webwm. I am not too familiar with these these, but they seem to work best for desktop web sites. Other 3rd party tools worth mentioning is of course the BONDI project.

Looking up the location of the end users IP is fall back for both Google Gears and the W3C API. You can however, make your own lookup and thereby not be dependent on javascript. has a few options, both free and commercial. I have tested the free GeoLite City. Its very easy; download the IP database and some API and off you go. The PHP code looks like this:

$gi = geoip_open(dirname(__FILE__) ."/GeoLiteCity.dat",GEOIP_STANDARD);
$record = geoip_record_by_addr($gi,$_SERVER[";REMOTE_ADDR";]);
LBS based on IP is, as mentioned, not very accurate. It is useless for providing detailed maps, route descriptions and find-your-nearest-atm-type applications. You need GPS for that. However, its great for whats-happening-close-to-me type applications where you need a city level accuracy of the users location. You can test GeoLite City here.

You can also ask the end user to provide a location. Google, among others, provide a geo coding tool. An API translating a human readable name to coordinates. The API can pin point you at any level of a curacy, country, city or street address. PHP code for this API:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "".urlencode($address)."&output=json&oe=utf8&sensor=false&key=yourkey");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$reps = curl_exec($ch); 
And now to the last example; asking the mobile operator for the users location. This method is of course very dependent on what the various mobile operators out there provide and allow. The biggest disadvantages with this method is that you in most cases need the end user's mobile phone number and that the user needs to be connected to internet through his mobile operators network. There are privacy issues attached to that, of course... So, if you want to implement the mobile operators way of doing location lookups, you need to decide which to target, and then do the required integrations to their various APIs which can be a challenging job. Or you can use an aggregator. Then you get away with only one API. In the example i have used Mobiletech which is an aggregator covering the Scandinavian operators.

In the "i-mode world" ther is a slightly different way of solving the issue. You just create a link like this <a href="device:location?url=http://server/location.cgi">locate me</a> and your server is provided with location. Read more on the wirelessfaq. GSMA are also working on a project trying to standardize the mobile operators APIs, including location lookups.

So, lots of ways to get the longitude and latitude. Useful for pin pointing a location on a map, but what if you need the users address? For example you could help the user fill in a order form (as we know, very cumbersome on mobile). Google provide a reverse geo coding service. Put the lat/long in, and get the street address out. Very neat.

These are a few of the options and APIs you have when creating a location aware mobile web application running in the browser. There are many more out there, so please feel free to share your favorite APIs, tools, tricks and services.

Location is truly putting meaning into the word "mobile"! And don't forget the LBS forum here at mobiforge.

Posted by jonarne - 07 Jun 2009

jonarne's picture

John Arne has seen the mobile world both from the inside of a Telco and outside as a content provider. Currently he works as Head of Product Development in Mobiletech AS which he also co founded.


Posted by roland 6 years ago

Great post!

The RIM Browser supports also GPS access via javascript:

<script type="text/javascript"> 
  function locationCB()
      document.write("Latitude "  + blackberry.location.latitude);
      document.write("Longitude " + blackberry.location.longitude);
      return true;
  if( window.blackberry && blackberry.location.GPSSupported) {
        document.write("GPS Supported");
    document.write("This Device doesn't support the Blackberry Location API");

More details at:

Posted by jonarne 6 years ago

Thanks Roland!
I'm not too familiar with BBs. :)

Posted by daniel.hunt 6 years ago

This kind of thing really needs to be put into a mobile JS library

Daniel Hunt

Posted by ronanhigg 6 years ago

Access, the developers of the Netfront browsers used on SonyEricssons will be supporting Javascript calls for location in the next version.

Although web (and wap) pages being able to request phone location is a great advancement towards delivering location based services, the reality is that the more compelling services are the ones that require ongoing background checkin of location (securely and with privacy issues addressed, of course).

This can only be adequately addressed today with native applications. Regrettably some not-so-smart phones won't support background apps (iPhone).

Ronan for Locle

Posted by whoisstan 6 years ago

Just saw your article, you might the library interesting:

Check the samples,docs and please leave feedback, the library is quite young and needs some assistance;)

Posted by jonarne 6 years ago

@WHOISSTAN nice! just what we need! thx :)

Posted by kanaga 6 years ago

It's nice information about find the geo location using the javascript.
I found the site for getting the location of an ip-address.
It's useful for me.

Posted by whoisstan 6 years ago

using ip-address to locate the user works to a certain degree, for mobile providers most ip-addresses resolve to a business address, so an IP address of an AT&T mobile user will point to the AT&T HQ but will not give indication to the users whereabouts, beyond the country of course. in case of a user using Opera Mobile the IP address will resolve to Norway since that is where their servers are located even if the user is using ePlus in germany. for users coming through wi-fi connection the location might have a better resolution. Makes sense?

Posted by Salva 6 years ago

Great post Jonarne,

as you mentioned the GSMA are also working on a project trying to standardize the mobile operators APIs, billing APIs, messaging APIs, and of course including a location API.

Salvatore, 25 Bytes

Posted by cranstone 6 years ago

An alternative approach is to use the X header format and simply add the "device data you need" to the outgoing HTTP request headers (via the mobile browser)


Peter J. Cranstone
CEO 5o9 Inc

Peter J. Cranstone
CEO 5o9 Inc