Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I automatically find a user's location?

I am currently working on a show listing website. I am going to display show information by location for the user sorted in a variety of different ways.

I know I could ask the user where they are located when they first sign into the site, but I've noticed that many sites have this capability built in to detect location automatically (Example, see Last.fm "Events: Concert Listings in your area").

How do they do this? I'm currently building my website in Ruby on Rails.

like image 464
CodingWithoutComments Avatar asked Feb 27 '09 22:02

CodingWithoutComments


2 Answers

Here's a link to the documentation on the relevant Google Maps API:

http://code.google.com/apis/ajax/documentation/#ClientLocation

It shows an example of how to use it:

/**
 * 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 United 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";
}

And tells you what you will get from it:

When populated, the google.loader.ClientLocation object is populated with the following metro-level granularity properties:

  • ClientLocation.latitude — supplies the low resolution latitude associated with the client's IP address

  • ClientLocation.longitude — supplies the low resolution longitude associated with the client's IP address

  • ClientLocation.address.city — supplies the name of the city associated with the client's IP address

  • ClientLocation.address.country — supplies the name of the country associated with the client's IP address

  • ClientLocation.address.country_code — supplies the name of the ISO 3166-1 country code associated with the client's IP address

  • ClientLocation.address.region — supplies the country specific region name associated with the client's IP address

like image 142
runako Avatar answered Sep 28 '22 08:09

runako


They usually do using IP resolution by IP tables. ip2nation might give some idea

like image 22
Codingday Avatar answered Sep 28 '22 10:09

Codingday