Click here to Skip to main content
15,919,613 members
Articles / Web Development / HTML

Localize a user with the JavaScript Geolocation API

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
20 May 2010CPOL1 min read 17.2K   4   10
The web in the future will contain more localized information than it already contains today. The Javascript Geolocation API contains the tools you need to localize a user of your web site.

Javascript provides a function to get the location of the current user of your web page. Currently it is only implemented in Firefox 3.5 and Safari mobile version. Also Chrome has implemented it, but it isn't turned on by default. I'm talking about the Geolocation API which is a W3C standard. It is amazing how accurate it works (in Switzerland). It normally localizes me with a 50 meters difference (about 150 feet). I build a simple web application which localizes you and shows the result on Google maps (already said, only works with Firefox 3.5 currently). It would be great if you could post how accurate it is in your country!

The Geolocation API provides a function to get the current location and another one to watch the location of the user. Both functions takes a function as input. The function which will be taken as input receives the position object, which contains the location information of the user.

JavaScript
//does the browser support the Geolocation API?
if(navigator.geolocation) 
{
   //will call setPosition once
   navigator.geolocation.getCurrentPosition(setPosition);

   //will call setPosition multiple times with the current position
   var id = navigator.geolocation.watchPosition(scrollMap);
}

function setPosition(position) 
{
   var latitude = position.coords.latitude;
   var longitude = position.coords.longitude
   // your code
}

The position object contains an attribute coordinate which stores longitude, latitude and altitude. But it also contains information about how accurate these position information are. The heading attribute, in the coordinate object, contains the direction of travel specified in degrees which is relative to north. Also a speed attribute is provided which contains the speed in meters of the device. The specification of the Coordinates interface looks as follows:

JavaScript
interface Coordinates {
  readonly attribute double latitude;
  readonly attribute double longitude;
  readonly attribute double? altitude;
  readonly attribute double accuracy;
  readonly attribute double? altitudeAccuracy;
  readonly attribute double? heading;
  readonly attribute double? speed;
};

The Geolocation API is a great thing to build web applications which shows location specific information. It is very accurate and it is easy to use with a map provider like Google or Bing. The web in the future will contain more localized information than it already contains today, so keep an eye on this API!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralBroken link/site Pin
Peter_in_278010-Aug-10 19:10
professionalPeter_in_278010-Aug-10 19:10 
GeneralRe: Broken link/site Pin
User 661920718-Aug-10 21:04
User 661920718-Aug-10 21:04 
Generalfixed Pin
User 661920718-Aug-10 21:18
User 661920718-Aug-10 21:18 
GeneralRe: fixed Pin
Peter_in_278019-Aug-10 18:22
professionalPeter_in_278019-Aug-10 18:22 
GeneralError on my location is a bit more Pin
Dennis Dykstra7-Jun-10 17:07
Dennis Dykstra7-Jun-10 17:07 
I'm in Portland, Oregon and in my case the location error is about 7 miles (about 11 km). Still, it's much better than with most of the locators that use IP information--usually those are off by several hundred miles and sometimes they show me on the East Cost of the US when in fact I'm on the West Coast.
Dennis Dykstra

GeneralI'm not in Palo Alto Pin
Peter_in_278024-May-10 15:16
professionalPeter_in_278024-May-10 15:16 
GeneralRe: I'm not in Palo Alto Pin
User 661920724-May-10 19:07
User 661920724-May-10 19:07 
GeneralRe: I'm not in Palo Alto Pin
Peter_in_278024-May-10 19:23
professionalPeter_in_278024-May-10 19:23 
GeneralRe: That "position" parameter... Pin
User 661920720-May-10 23:07
User 661920720-May-10 23:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.