31

有谁知道我可以调用任何开放的 RESTful API 来将用户的 IP 地理编码为纬度和经度?

理想情况下,它将类似于: http: //google.com/geocode_api/ ?IP= 1.2.3.4,它将返回纬度和经度。

4

5 回答 5

23

另一个具有城市准确信息的免费 REST API 是http://freegeoip.net请求相当直接。你会使用类似的东西

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

对 IP 地址进行地理编码,其中格式可以csvxmljson. 他们的网站有所有的细节。

[更新:] FreeGeoIP.net 过去并没有作为公共服务持续提供。但是,该软件始终是开源的,并且可以在Github上获得。如果您需要高度可靠的服务或您的用例超过当前 15.000 个请求/小时的配额,则使用 Docker 运行本地安装相当容易。

于 2013-06-20T08:33:44.283 回答
8

这里有几个简单的电话......

示例调用:-

返回的 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> 
于 2010-07-12T21:55:08.210 回答
7

您可以使用谷歌 API: http ://code.google.com/apis/ajax/documentation/#ClientLocation

编辑

例子:

<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"); }
 }

​</p>

于 2010-07-12T21:49:24.720 回答
7

在我的网站上,我使用http://ip-api.com/从 IP 地址获取位置。他们有很好的限制(每分钟最多 150 个请求)。Ipinfo.io 每天只有少于 1000 个请求是免费

这是示例输出:

(
    [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
)

这是您可以使用的 PHP 代码:

$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
于 2015-09-18T18:03:45.153 回答
2

您可以在此处找到始终更新的免费地理数据库 http://www.maxmind.com/app/geolitecity

你可以创建一个新的 C# 服务来使用这个 Geo DB,比如 http://www.maxmind.com/app/csharp

您可以通过以下链接在线尝试 http://www.maxmind.com/app/lookup_city

于 2012-04-16T14:46:54.120 回答