5

Has anyone been able to get geo-location based on a person's IP to work using Google Maps API v3 JavaScript?

It seems to me that even the google provided example doesn't work.

http://gmaps-samples-v3.googlecode.com/svn/trunk/commonloader/clientlocation.html

Question:

  1. Does this example work for anyone?

  2. How do I get geolocation based on a person's IP to work using Google Maps API v3?

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
Teddy
  • 59
  • 1
  • 1
  • 2

2 Answers2

5

Q1: It works from here, and probably from many other locations. However note that geolocation from IP addresses is not a very reliable science. You will be getting the location of your ISP, which can be quite far away, and in addition the IP-to-location databases aren't always up to date with the latest changes, so you might not have any data for a particular IP address -- which is probably what is happening in your case.

MaxMind, which offers a popular IP-to-location database published some statistics on its database:

Q2: The only way to get the geolocation from an IP address through the Google Maps API v3 is by using the same method used in the example you provided. However if you find that any other geolocation database, like MaxMind GeoLite City, is more accurate for your country, you may want to do the geolocation from the IP Addresses yourself, instead of delegating it to Google Maps.

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
3

The web Google Maps API doesn't seem to offer an IP address geolocation service (the provided example suggests using the W3C Geolocation standard, which typically requires an action from the user).

However! Google's Maps Geolocation API, typically used on mobile clients, can be used from the web and does return a latitude & longitude based on the requesting client's IP address.

Here's a quick jQuery example that demonstrates its use:

  $.ajax({
    url: 'https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR-API-KEY',
    data: JSON.stringify({ "considerIp": "true" }),
    type: 'POST',
    contentType: 'application/json',
    success: function(data) {
      if(data.location) {
        alert(data.location.lat + ', ' + data.location.lng);
      } else {
        alert('not found');
      }
    }
  });

Here's the curl equivalent:

curl -H "Content-Type: application/json" -X POST -d '{"considerIp": true}' https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR-API-KEY

Don't forget to swap in a real API key in the examples above AND to enable the Geolocation API for your Google API project.

Greg Sadetsky
  • 4,863
  • 1
  • 38
  • 48
  • Any chance you can provide an html example? – Rocco The Taco Nov 01 '17 at 15:39
  • 1
    @RoccoTheTaco: here's [an example](http://jsbin.com/fiberadufu/1/edit?html,console,output) using the same code as above. You will need to set the 'GOOGLE_API_KEY' variable, otherwise this won't work (go to the Google API Console, create a new project, enable the Geolocation API, and get an API key). – Greg Sadetsky Nov 01 '17 at 20:00
  • Check the [examples](https://developers.google.com/maps/documentation/javascript/geolocation) in the Google Maps website. – Jaime Nov 28 '17 at 03:10
  • Isn't this dangerous, since you put your API key in the front-end? – Wiezalditzijn Jun 06 '23 at 13:03
  • 1
    @Wiezalditzijn that kind of API key is not private and is meant to be read / used in the frontend. you can restrict how/where it's used though which is a good practice -- see [here](https://cloud.google.com/blog/products/maps-platform/google-maps-platform-best-practices-restricting-api-keys) for docs. – Greg Sadetsky Jun 06 '23 at 22:25