📜 ⬆️ ⬇️

Where is my user from?

Did you have to use such a chip 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 exactly an API, it’s all just a property of the google.loader.ClientLocation object.
After downloading any part of the Google AJAX API, the system will try to find the geographical location of the client by its ip-address.
If there is a correspondence between the maps and the ip-address of the client, then google.loader.ClientLocation returns the approximate coordinates of the country, region, city of the user. Mapping is far from perfect, but it allows you to use the “smart” default settings or show your visitor location awareness in your projects.
Here is a simple google.loader.ClientLocation example:

/**
* 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 States Elections page . If you go to the “News by State” section, you will see either the news about the candidate from the state you are in, or, if the coordinates on the map do not match with your ip address, the news about the candidate from the state of California.
Now about the pleasant. If you like the mechanism for mapping the ip-address to the visitor's location, then you should wait for what the Gears team 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.

')

Source: https://habr.com/ru/post/43090/


All Articles