简体   繁体   English

如何从IP地址整数值获取位置详细信息

[英]How to get location details from IP address integer value

I am converting IP address in to its corresponding number. 我正在将IP地址转换为其相应的号码。 Now I want to know the location details ie long and lat, time zone etc. Is it possible in Java. 现在,我想知道位置详细信息,即长和纬度,时区等。在Java中是否可能? Do i have to use some external jar for database ? 我必须使用一些外部jar来存储数据库吗?

IP addresses themselves don't have location info. IP地址本身没有位置信息。 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. 为此,有许多API和数据库可以连接,并且您可以选择在本地下载此类信息以避免远程查找。

So as others have correctly pointed out an IP address does not contain any of this 'metadata' about location. 因此,正如其他人正确指出的那样,IP地址不包含任何有关位置的“元数据”。 You will need to either rely on a third party to get this information, retrieve this information yourself, or go without it. 您将需要依靠第三方来获取此信息,自己检索此信息或不使用它。 Writing the library to scrape that information should be possible in Java. 编写库以抓取该信息应该可以使用Java。

  ipAddress = request.getRemoteAddr();
  GeoLocation gl = new GeoLocation();
  gl.GetGeoLocationByIP(ipAddress);

String country = gl.Country;

refer 参考

For getting location from IP address you have to buy the IP databases where they will periodically update the IP table. 为了从IP地址获取位置,您必须购买IP数据库,这些数据库将定期更新IP表。

However if you want, you can get the location details viz city,region,country,longitude,latitude from request header. 但是,如果需要,您可以从请求标头中获取位置详细信息,即城市,区域,国家,经度,纬度。

It's only available for GAE user. 仅适用于GAE用户。

For more details go through GAE ReleaseNotes 有关更多详细信息,请访问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;
}

you can get address of your service provider. 您可以获得服务提供商的地址。 Not the owner of phone. 不是手机的所有者。

if you r using android which is GPS enabled. 如果您使用的是启用GPS的android。 then u can get your exact position. 那么你可以得到你的确切位置。 Code for that is given below link. 链接下面给出了代码。

What is the simplest and most robust way to get the user's current location on Android? 在Android上获取用户当前位置的最简单,最可靠的方法是什么?

You can use my API to lookup this information, https://ipinfo.io . 您可以使用我的API来查找此信息,即https://ipinfo.io If you don't pass in an IP address it'll give you details for your own one, or you can pass an IP address to get details for that one: 如果您不传递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"
}

See https://ipinfo.io/developers for more details. 有关更多详细信息,请参见https://ipinfo.io/developers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM