有没有办法使用Google API在Live time中获取我的用户的IP地理位置?
我认为它将使用分析数据库,这是唯一一个,跟踪我的城市级别的用户,实际上是正确的(任何其他IP -位置-API,我可以测试显示我的IP地址接近200公里,我的真实位置。谷歌显示它有2亿(!)离开!)
我想知道我的用户的位置(在浏览器端,并将其传输到我的服务器或服务器端),以提供与城市相关的内容。但我不想让我的用户看到这些烦人的弹出窗口,要求使用GPS,所以我想使用IP地址。
有什么建议吗?
发布于 2012-12-10 09:12:24
如果你不想使用HTML5 style client-enabled GeoIP information,你将需要一个像MaxMind's GeoIP Lite database这样的GeoIP数据库,它是免费的,适用于99%的用例。任何其他具有更准确/详细信息的服务都将花费您一大笔钱。MaxMind得到了很多人的好评,很好地满足了我个人的需求。它可以为您提供Country/Region/City/Latitude-Longitude-Coordinates/Continent信息。
发布于 2018-05-29 02:58:17
您可以使用Google的地理定位API根据用户的IP地址获取最新和最新信息:
var apiKey = "Your Google API Key";
function findLatLonFromIP() {
return new Promise((resolve, reject) => {
$.ajax({
url: `https://www.googleapis.com/geolocation/v1/geolocate?key=${apiKey}`,
type: 'POST',
data: JSON.stringify({considerIp: true}),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: (data) => {
if (data && data.location) {
resolve({lat: data.location.lat, lng: data.location.lng});
} else {
reject('No location object in geolocate API response.');
}
},
error: (err) => {
reject(err);
},
});
});
}然后,您可以使用这些坐标通过地理编码API获取用户的地址。下面是一个返回国家/地区的示例:
function getCountryCodeFromLatLng(lat, lng) {
return new Promise((resolve, reject) => {
$.ajax({
url: `https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&key=${apiKey}`,
type: 'GET',
data: JSON.stringify({considerIp: true}),
dataType: 'json',
success: (data) => {
console.log('reverse geocode:', data.results[0].address_components);
data.results.some((address) => {
address.address_components.some((component) => {
if (component.types.includes('country')) {
return resolve(component.short_name);
}
});
});
reject('Country not found in location information.');
},
error: (err) => {
reject(err);
},
});
});
}在上面,只需通过data.results查找所需的信息(城市、街道、国家等)同时使用上面的两个函数:
findLatLonFromIP().then((latlng) => {
return getCountryCodeFromLatLng(latlng.lat, latlng.lng);
}).then((countryCode) => {
console.log('User\'s country Code:', countryCode);
});发布于 2012-12-10 09:12:04
您可以使用Google的地理编码API来获取某个位置的实际地址,但该API所需的输入是纬度和经度坐标。
示例:http://maps.googleapis.com/maps/api/geocode/json?latlng=43.473,-82.533&sensor=false
您需要从其他供应商查找和IP到Location API,才能到达城市级别,或者保留提示他们授予您访问其地理位置的权限的选项。
IPInfoDB在不使用输入的情况下通过IP自动缩小位置范围方面做得非常好:
http://ipinfodb.com/ip_location_api.php
https://stackoverflow.com/questions/13793655
复制相似问题