有没有人知道我可以调用任何开放的RESTful API来将用户的IP地理编码为纬度和经度?
理想情况下,它会是:http://google.com/geocode_api/?IP=1.2.3.4,它会返回纬度和经度。
答案 0 :(得分:21)
另一个具有城市准确信息的免费REST API将是http://freegeoip.net请求非常简单。你会使用像
这样的东西http://freegeoip.net/{format}/{ip_or_hostname}
对IP地址进行地理编码,其格式可以是csv
,xml
或json
。他们的网站有所有细节。
[更新:] FreeGeoIP.net过去并未持续作为公共服务提供。但是,该软件始终是开源的,可在Github上获得。如果您需要高度可靠的服务或者您的用例超过当前每小时15.000个请求的配额,那么使用Docker运行本地安装相当容易。
答案 1 :(得分: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>
答案 2 :(得分:7)
您可以使用Google 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"); }
}
答案 3 :(得分:6)
在我的网站上,我使用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
答案 4 :(得分:2)
您可以在此处找到始终更新的免费地理数据库 http://www.maxmind.com/app/geolitecity
你可以创建一个新的C#服务来使用这个Geo DB http://www.maxmind.com/app/csharp
您可以通过以下链接在线试用 http://www.maxmind.com/app/lookup_city