google.loader.ClientLocation to get a person's lat/long using their IP address


/ Published in: JavaScript
Save to your folder(s)

Set the center of the map on the person's location based on IP address. In this case the user is required to be in the USA.


Copy this code and paste it in your HTML
  1. if (GBrowserIsCompatible()) {
  2. var map = new google.maps.Map2(document.getElementById("my-googlemap"));
  3. if (google.loader.ClientLocation &&
  4. google.loader.ClientLocation.address.country_code == "US" &&
  5. google.loader.ClientLocation.address.region) {
  6. // geo locate was successful and user is in the states
  7. map.setCenter(
  8. new google.maps.LatLng(google.loader.ClientLocation.latitude,
  9. google.loader.ClientLocation.longitude
  10. ), 8
  11. );
  12. // do the rest of your map stuff here
  13. }
  14. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.