Geocode an IP address? [closed]
Asked Answered
D

5

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.

Devi answered 12/7, 2010 at 21:44 Comment(5)
possible duplicate of How to determine a zip code and city from an IP address?Heaney
@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.Devi
why do you think the answer is different? Do the referenced APIs not return latitude and longitude?Heaney
IPLocate.io provides a free API: https://www.iplocate.io/api/lookup/8.8.8.8 - Disclaimer: I run this service.Tody
Ipregistry: api.ipregistry.co/8.8.8.8?key=tryoutUtile
R
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.

Revenant answered 20/6, 2013 at 8:33 Comment(6)
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.Conceptualism
As of 2/20/2016 the freegeoip.net service seems to be working fine.Gyro
How can I send multiple IP addresses to this website??Necessaries
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."Revenant
AS of 3/23/2016 the freegeoip.net is not workingEzraezri
I found installing freegeoip locally extremely easy and it circumvents the remote server issues github.com/fiorix/freegeoip/releasesGyro
C
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> 
Cay answered 12/7, 2010 at 21:55 Comment(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.Conceptualism
Dead links no longer validWeeds
V
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"); }
 }

Virchow answered 12/7, 2010 at 21:49 Comment(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 withinDevi
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 .Virchow
So how can I load JUST ClientLocation without any other modules?Devi
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"Virchow
See discussion here -- unfortunately, this API is not supported anymore (just tested it and google.loader.ClientLocation does not exist).Honshu
Just found another working Google API for geolocating IP addresses! See this answer.Honshu
C
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
Cytologist answered 18/9, 2015 at 18:3 Comment(3)
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.Declinate
tnks @ValentineKonov, I have updated the answer.Cytologist
Just to confirm: contrarily to some of the listed above this one is still up and running. Provides pretty comprehensive feeds.Alagez
H
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

Hinckley answered 16/4, 2012 at 14:46 Comment(2)
non of these return the lat/long position using the IP addressTatar
@geo171 : I Think you'll need one of below sites to get the lat/long Service : api.hostip.info/get_html.php?ip=12.215.42.19&position=true Sites: digitalpoint.com/tools/geovisgeobytes.com/IpLocator.htm melissadata.com/lookups/iplocip2location.com/free.asp hostip.info networldmap.com/TryIt.htm private.org.il/IP2geo.html internetfrog.com webtools.live2support.com/misc_loseomoz.org/ip2loc/ip2loc.php webyield.net/domainquery.htmlHinckley

© 2022 - 2024 — McMap. All rights reserved.