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.

Tim
  • 311
  • 1
  • 3
  • 3
  • possible duplicate of [How to determine a zip code and city from an IP address?](http://stackoverflow.com/questions/348614/how-to-determine-a-zip-code-and-city-from-an-ip-address) – John Saunders Jul 12 '10 at 21:48
  • 4
    @John Saunder, I actually don't care what zip code or city the user is in. I simply want to know their longitude and latitude so that I can center my map to be on their home location. – Tim Jul 12 '10 at 21:52
  • why do you think the answer is different? Do the referenced APIs not return latitude and longitude? – John Saunders Jul 12 '10 at 21:55
  • IPLocate.io provides a free API: [`https://www.iplocate.io/api/lookup/8.8.8.8`](https://www.iplocate.io/api/lookup/8.8.8.8) - Disclaimer: I run this service. – ttarik Nov 15 '17 at 01:01
  • **Ipregistry**: https://api.ipregistry.co/8.8.8.8?key=tryout – Laurent Jun 27 '19 at 13:37

5 Answers5

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.

Stefan Haberl
  • 9,812
  • 7
  • 72
  • 81
  • 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 '14 at 19:40
  • 1
    As of 2/20/2016 the freegeoip.net service seems to be working fine. – cengel Feb 20 '16 at 18:42
  • How can I send multiple IP addresses to this website?? – Eray Balkanli Mar 06 '16 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." – Stefan Haberl Mar 07 '16 at 08:52
  • 2
    AS of 3/23/2016 the freegeoip.net is not working – user123456 Mar 23 '16 at 13:27
  • I found installing freegeoip locally extremely easy and it circumvents the remote server issues https://github.com/fiorix/freegeoip/releases – cengel Apr 28 '16 at 21:29
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> 
Andy Robinson
  • 7,309
  • 2
  • 30
  • 20
  • 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 '14 at 19:40
  • Dead links no longer valid – Rocco The Taco Nov 01 '17 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"); }
 }

Josiah
  • 4,754
  • 1
  • 20
  • 19
  • 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 '10 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 '10 at 22:04
  • So how can I load JUST ClientLocation without any other modules? – Tim Jul 12 '10 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 '10 at 22:36
  • See [discussion here](http://stackoverflow.com/questions/14195837/is-google-loader-clientlocation-still-supported) -- unfortunately, this API is not supported anymore (just tested it and google.loader.ClientLocation does not exist). – Greg Sadetsky Oct 14 '16 at 17:20
  • 1
    Just found another working Google API for geolocating IP addresses! See this [answer](http://stackoverflow.com/a/40049684/426790). – Greg Sadetsky Oct 14 '16 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
Jasom Dotnet
  • 1,225
  • 12
  • 17
  • 2
    Limit now is 150, and they ban you if you exceed the limits ([proof](http://ip-api.com/docs/)) `Our system will automatically ban any IP addresses doing over 150 requests per minute. To unban your IP click here.` – Valentine Konov Mar 12 '16 at 14:28
  • tnks @ValentineKonov, I have updated the answer. – Jasom Dotnet Jun 15 '16 at 07:46
  • Just to confirm: contrarily to some of the listed above this one is still up and running. Provides pretty comprehensive feeds. – Adam Bubela Aug 18 '16 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

Tarek El-Mallah
  • 4,015
  • 1
  • 31
  • 46
  • non of these return the lat/long position using the IP address – George Filippakos Mar 04 '13 at 14:12
  • @geo171 : I Think you'll need one of below sites to get the lat/long Service : http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true Sites: http://www.digitalpoint.com/tools/geovis… http://www.geobytes.com/IpLocator.htm http://www.melissadata.com/lookups/iploc… http://www.ip2location.com/free.asp http://www.hostip.info/ http://www.networldmap.com/TryIt.htm http://www.private.org.il/IP2geo.html http://www.internetfrog.com/ http://webtools.live2support.com/misc_lo… http://www.seomoz.org/ip2loc/ip2loc.php http://www.webyield.net/domainquery.html – Tarek El-Mallah Mar 05 '13 at 16:17