[英]How to get location details from IP address integer value
我正在将IP地址转换为其相应的号码。 现在,我想知道位置详细信息,即长和纬度,时区等。在Java中是否可能? 我必须使用一些外部jar来存储数据库吗?
IP地址本身没有位置信息。 但是,存在许多具有此映射的数据库/服务。
有关许多其他选项,请参见此博客条目 。 为此,有许多API和数据库可以连接,并且您可以选择在本地下载此类信息以避免远程查找。
因此,正如其他人正确指出的那样,IP地址不包含任何有关位置的“元数据”。 您将需要依靠第三方来获取此信息,自己检索此信息或不使用它。 编写库以抓取该信息应该可以使用Java。
ipAddress = request.getRemoteAddr();
GeoLocation gl = new GeoLocation();
gl.GetGeoLocationByIP(ipAddress);
String country = gl.Country;
为了从IP地址获取位置,您必须购买IP数据库,这些数据库将定期更新IP表。
但是,如果需要,您可以从请求标头中获取位置详细信息,即城市,区域,国家,经度,纬度。
仅适用于GAE用户。
有关更多详细信息,请访问GAE版本说明
public static HashMap<String, Object> getLocationDetails(HttpServletRequest request)
{
HashMap<String, Object> locationMap = null;
String country = "",city="",region="",latitude="",longitude="",temp="",state="",title="",address="",zip="";
boolean primary = true;
try
{
locationMap = new HashMap<String, Object>();
country = request.getHeader("X-AppEngine-Country");
region = request.getHeader("X-AppEngine-Region");
city = request.getHeader("X-AppEngine-City");
temp = request.getHeader("X-AppEngine-CityLatLong");
latitude = (temp.split(","))[0];
longitude = (temp.split(","))[1];
log.info("country>>"+country+"region>>"+region+"city>>"+city+"temp>>"+temp+"latitude>>"+latitude+"longitude>>"+longitude);
locationMap.put("city" , city);
locationMap.put("state" , region);
locationMap.put("country" , country);
locationMap.put("latitude" , latitude);
locationMap.put("longitude" , longitude);
log.info("locationMap==>"+locationMap);
}catch (Exception e)
{
log.log(Level.SEVERE,"Exception while getting location"+e.getMessage(),e);
}
return locationMap;
}
您可以获得服务提供商的地址。 不是手机的所有者。
如果您使用的是启用GPS的android。 那么你可以得到你的确切位置。 链接下面给出了代码。
您可以使用我的API来查找此信息,即https://ipinfo.io 。 如果您不传递IP地址,则会为您提供自己的IP地址的详细信息,也可以传递IP地址以获取该IP地址的详细信息:
$ curl ipinfo.io/24.6.61.239
{
"ip": "24.6.61.239",
"hostname": "c-24-6-61-239.hsd1.ca.comcast.net",
"city": "Mountain View",
"region": "California",
"country": "US",
"loc": "37.3845,-122.0881",
"org": "AS7922 Comcast Cable Communications, LLC",
"postal": "94040"
}
有关更多详细信息,请参见https://ipinfo.io/developers 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.