Where is my user from?

Published on October 24, 2008

Where is my user from?

Original author: Mark Lucovsky, Software Engineer
  • Transfer
Have you ever used such a feature in your web application as centering the Google map at the user's location? Consider a small example with a map. True, it was great when displaying a map so that it showed the country, region, city, etc. of the current user, and not just centered on Los San Francisco or Australia?

Take a look at the new API. Actually, this is not an API, it's just a property of the google.loader.ClientLocation object.
After loading any part of the Google AJAX API, the system will try to find the geographic location of the client by its IP address.
If there is a correspondence between the cards and the client’s IP address, then google.loader.ClientLocation returns the approximate coordinates of the country, region, city of the user. Mapping is far from ideal, but it allows you to use “smart” default settings or to show your awareness of the visitor’s location in your projects.
Here is a simple example of using google.loader.ClientLocation:

* Set the currentState_ and currentCountry_ properties based on the client's
* current location (when available and in the US), or to the defaults.
InTheNews.prototype.setDefaultLocation_ = function() {
 this.currentState_ = this.options_.startingState;
 if (google.loader.ClientLocation &&
  google.loader.ClientLocation.address.country_code == "US" &&
  google.loader.ClientLocation.address.region) {

  // geo locate was successful and user is in the states. range check
  // the region so that we can safely use it when selecting a state
  // level polygon overlay
  var state = google.loader.ClientLocation.address.region.toUpperCase();
  if (InTheNews.stateNames[[]state]) {
   this.currentState_ = state;
 this.currentCountry_ = "US";

* This source code was highlighted with Source Code Highlighter.

To make sure the code works, visit the 2008 United States Election Page . If you go to the “News by State” section, you will see either news about the candidate from the state where you are located, or if there is no correspondence of the coordinates on the map with your IP address, news about the candidate from the state of California.
Now for the pleasant. If you liked the mechanism for mapping the IP address to the visitor’s location, then you should wait for what the Gears team has prepared for you . They are preparing the final version of this functionality, which can determine the coordinates by ip-address, GPS and soon WiFi. This is definitely a leap in the direction of improving both functionality and accuracy.