0

我正在将 IP 地址转换为相应的数字。现在我想知道位置详细信息,即经纬度、时区等。在 Java 中是否可能。我必须为数据库使用一些外部 jar 吗?

4

4 回答 4

2

IP addresses themselves don't have location info. However a number of databases/services exist that have this mapping.

See this blog entry for a number of different options. There are a number of APIs and databases to connect to for this, and you have the option of downloading such info locally to avoid remote lookups.

于 2012-07-20T10:55:22.227 回答
1

要从 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;
}
于 2013-08-03T08:22:42.643 回答
1
  ipAddress = request.getRemoteAddr();
  GeoLocation gl = new GeoLocation();
  gl.GetGeoLocationByIP(ipAddress);

String country = gl.Country;

参考

于 2013-05-08T06:33:38.120 回答
1

因此,正如其他人正确指出的那样,IP 地址不包含任何有关位置的“元数据”。您将需要依靠第三方来获取此信息,自己检索此信息,或者不使用它。在 Java 中编写库来抓取这些信息应该是可能的。

于 2012-07-20T11:14:44.987 回答