Javascript Google Maps API v3 - 基于 IP 的地理定位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2083598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Maps API v3 - IP-based Geolocation
提问by Teddy
Has anyone been able to get geo-location based on a person's IP to work using Google Maps API v3 JavaScript?
有没有人能够使用 Google Maps API v3 JavaScript 根据一个人的 IP 获取地理位置?
It seems to me that even the google provided example doesn't work.
在我看来,即使是谷歌提供的例子也不起作用。
http://gmaps-samples-v3.googlecode.com/svn/trunk/commonloader/clientlocation.html
http://gmaps-samples-v3.googlecode.com/svn/trunk/commonloader/clientlocation.html
Question:
问题:
Does this example work for anyone?
How do I get geolocation based on a person's IP to work using Google Maps API v3?
这个例子对任何人都有效吗?
如何使用 Google Maps API v3 获取基于个人 IP 的地理定位?
回答by Daniel Vassallo
Q1:It works from here, and probably from many other locations. However note that geolocation from IP addresses is not a very reliable science. You will be getting the location of your ISP, which can be quite far away, and in addition the IP-to-location databases aren't always up to date with the latest changes, so you might not have any data for a particular IP address -- which is probably what is happening in your case.
Q1:它可以从这里运行,也可能从许多其他位置运行。但是请注意,从 IP 地址进行地理定位并不是一门非常可靠的科学。您将获得 ISP 的位置,该位置可能很远,此外,IP 到位置的数据库并不总是与最新更改保持同步,因此您可能没有特定 IP 的任何数据地址——这可能就是你的情况。
MaxMind, which offers a popular IP-to-location database published some statistics on its database:
MaxMind 提供了一个流行的 IP 到位置数据库,在其数据库上发布了一些统计数据:
Q2:The only way to get the geolocation from an IP address through the Google Maps API v3 is by using the same method used in the example you provided. However if you find that any other geolocation database, like MaxMind GeoLite City, is more accurate for your country, you may want to do the geolocation from the IP Addresses yourself, instead of delegating it to Google Maps.
Q2:通过 Google Maps API v3 从 IP 地址获取地理位置的唯一方法是使用您提供的示例中使用的相同方法。但是,如果您发现任何其他地理定位数据库(如MaxMind GeoLite City)对您所在的国家/地区更准确,您可能希望自己从 IP 地址进行地理定位,而不是将其委托给 Google 地图。
回答by Greg Sadetsky
The web Google Maps API doesn't seem to offer an IP address geolocation service (the provided examplesuggests using the W3C Geolocation standard, which typically requires an action from the user).
Web Google Maps API 似乎不提供 IP 地址地理定位服务(提供的示例建议使用 W3C 地理定位标准,这通常需要用户的操作)。
However! Google's Maps Geolocation API, typically used on mobile clients, canbe used from the web and doesreturn a latitude & longitude based on the requesting client's IP address.
然而!Google 的Maps Geolocation API,通常用于移动客户端,可以从网络使用,并根据请求客户端的 IP 地址返回纬度和经度。
Here's a quick jQuery example that demonstrates its use:
这是一个演示其用法的快速 jQuery 示例:
$.ajax({
url: 'https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR-API-KEY',
data: JSON.stringify({ "considerIp": "true" }),
type: 'POST',
contentType: 'application/json',
success: function(data) {
if(data.location) {
alert(data.location.lat + ', ' + data.location.lng);
} else {
alert('not found');
}
}
});
Here's the curlequivalent:
这是curl等效的:
curl -H "Content-Type: application/json" -X POST -d '{"considerIp": true}' https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR-API-KEY
Don't forget to swap in a real API key in the examples above AND to enable the Geolocation APIfor your Google API project.
不要忘记在上面的示例中交换真实的 API 密钥,并为您的 Google API 项目启用 Geolocation API。

