31

Does anyone know of any open RESTful API that I can call to geocode a user's IP to the latitude and longitude?

Ideally, it would be something like: http://google.com/geocode_api/?IP=1.2.3.4 and it would return the latitude and longtitude.

5

5 Answers 5

23

Another free REST API with city accurate information would be http://freegeoip.net Requests are fairly straight forward. You would use something like

http://freegeoip.net/{format}/{ip_or_hostname}

to geocode an IP address, where format can be csv, xml or json. Their website has all the details.

[UPDATE:] FreeGeoIP.net was not continuously available in the past as a public service. The software was, however, always open source and is available on Github. It's fairly easy to get your local installation running using Docker, if you need a highly reliable service or your use case exceeds the current quota of 15.000 requests/hour.

6
  • 2
    As of 12/30/2014 it seems as though the freegeoip.net service is no longer working. However the ipinfodb.com service from Andy's response is still available.
    – hooknc
    Dec 30, 2014 at 19:40
  • 1
    As of 2/20/2016 the freegeoip.net service seems to be working fine.
    – cengel
    Feb 20, 2016 at 18:42
  • How can I send multiple IP addresses to this website?? Mar 6, 2016 at 20:27
  • I guess you have to make multiple requests. Note their quota limits, though: "You're allowed up to 10,000 queries per hour by default. Once this limit is reached, all of your requests will result in HTTP 403, forbidden, until your quota is cleared." Mar 7, 2016 at 8:52
  • 2
    AS of 3/23/2016 the freegeoip.net is not working
    – user123456
    Mar 23, 2016 at 13:27
8

Here's a couple with simple calls...

Example calls :-

Example of returned XML (ipinfodb) :-

<Response> 
  <Ip>122.169.8.137</Ip> 
  <Status>OK</Status> 
  <CountryCode>IN</CountryCode> 
  <CountryName>India</CountryName> 
  <RegionCode>10</RegionCode> 
  <RegionName>Haryana</RegionName> 
  <City>Kaul</City> 
  <ZipPostalCode></ZipPostalCode> 
  <Latitude>29.85</Latitude> 
  <Longitude>76.6667</Longitude> 
  <Timezone>0</Timezone> 
  <Gmtoffset>0</Gmtoffset> 
  <Dstoffset>0</Dstoffset> 
</Response> 
2
  • As of 12/30/2014 it seems as though the freegeoip.net service is no longer working. However the ipinfodb.com service is still available, however, it does look like they have changed their api url.
    – hooknc
    Dec 30, 2014 at 19:40
  • Dead links no longer valid Nov 1, 2017 at 15:20
7

You could use the google API: http://code.google.com/apis/ajax/documentation/#ClientLocation

Edit

Example:

<script type="text/javascript"
    src="http://www.google.com/jsapi?key=ABCDEFG"></script>
<script type="text/javascript">
google.load("maps", "2.x", {callback: initialize});

function initialize() {
  if (google.loader.ClientLocation) {
      var lat = google.loader.ClientLocation.latitude;
      var long = google.loader.ClientLocation.longitude;
      alert ("lat: " + lat + "\nlong: " + long);
   }
   else { alert ("not available"); }
 }

6
  • Can I use that without loading the entire Google Map framework? Reading the docs, it's unclear to me what module google.loader.ClientLocation exists within
    – Tim
    Jul 12, 2010 at 21:53
  • It doesn't exist within any of the API modules, it's a part of the API loader framework. It's a sister command to google.load .
    – Josiah
    Jul 12, 2010 at 22:04
  • So how can I load JUST ClientLocation without any other modules?
    – Tim
    Jul 12, 2010 at 22:06
  • I've added example code. But it looks like you actually have to load a module to populate ClientLocation. You could load the search API instead which is relatively small and probably already exists within a user's cache. To load the search api change "maps" to "search" and "2.x" to "1"
    – Josiah
    Jul 12, 2010 at 22:36
  • 1
    Just found another working Google API for geolocating IP addresses! See this answer. Oct 14, 2016 at 18:21
7

On my site I use http://ip-api.com/ for getting location from IP address. They have nice limits (up to 150 request per minute). Ipinfo.io is free only for less then 1000 requests per day.

This is sample output:

(
    [as] => AS8075 Microsoft Corporation
    [city] => Redmond
    [country] => United States
    [countryCode] => US
    [isp] => Microsoft bingbot
    [lat] => 47.674
    [lon] => -122.1215
    [org] => Microsoft bingbot
    [query] => 157.55.39.67
    [region] => WA
    [regionName] => Washington
    [status] => success
    [timezone] => America/Los_Angeles
    [zip] => 98052
)

This is PHP code you can use:

$ip = $_SERVER['REMOTE_ADDR'];
$result = json_decode(file_get_contents("http://ip-api.com/json/{$ip}"));
//print_r ($result);
echo "{$result->lat},{$result->lon}";//48.156,17.142
3
  • 2
    Limit now is 150, and they ban you if you exceed the limits (proof) Our system will automatically ban any IP addresses doing over 150 requests per minute. To unban your IP click here. Mar 12, 2016 at 14:28
  • tnks @ValentineKonov, I have updated the answer. Jun 15, 2016 at 7:46
  • Just to confirm: contrarily to some of the listed above this one is still up and running. Provides pretty comprehensive feeds. Aug 18, 2016 at 11:21
2

You can find a FREE Geo database always updated here http://www.maxmind.com/app/geolitecity

and you can create a new C# service to use this Geo DB like http://www.maxmind.com/app/csharp

you can try it online with below link http://www.maxmind.com/app/lookup_city

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.