Kumar Shorav
Kumar Shorav

Reputation: 531

How to get location details from IP address integer value

I am converting IP address in to its corresponding number. Now I want to know the location details i.e. long and lat, time zone etc. Is it possible in Java. Do i have to use some external jar for database ?

Upvotes: 0

Views: 3824

Answers (4)

lambodar
lambodar

Reputation: 3783

For getting location from IP address you have to buy the IP databases where they will periodically update the IP table.

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.

For more details go through GAE ReleaseNotes

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;
}

Upvotes: 1

Srinivasu
Srinivasu

Reputation: 1235

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

String country = gl.Country;

refer

Upvotes: 1

Woot4Moo
Woot4Moo

Reputation: 24336

So as others have correctly pointed out an IP address does not contain any of this 'metadata' about location. 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.

Upvotes: 1

Brian Agnew
Brian Agnew

Reputation: 272437

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.

Upvotes: 2

Related Questions