Geocode an IP address? [closed]

拜拜、爱过 提交于 2019-11-27 11:21:56

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...

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> 

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!