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.
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 addressClientLocation.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
They usually do using IP resolution by IP tables. ip2nation might give some idea
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With