Geocode an IP address? [closed]
We don’t allow questions seeking recommendations 开发者_如何学编程for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this questionDoes 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.
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.
Here's a couple with simple calls...
- http://ipinfodb.com/ip_query.php
- http://freegeoip.appspot.com/
Example calls :-
- http://freegeoip.appspot.com/xml/122.169.8.137
- http://ipinfodb.com/ip_query.php?ip=122.169.8.137
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>
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"); }
}
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
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
精彩评论