5

I am looking for the sample code to get Exact Location of IP. I did goggling but haven't find any Sample code. Thank You..

3
  • let me test. nullPainter Commented Apr 24, 2013 at 7:22
  • Note that the free versions of the MaxMind databases are updated less regularly than the commercial versions. Also note that the notion of 'exact location' is relative when it comes to IP and may be based on the city where your ISP is based, depending on the size of your country, ISP etc. Commented Apr 24, 2013 at 7:29
  • check the ip2asn2cc library: github.com/AxLabs/ip2asn2cc Commented May 13, 2020 at 23:23

1 Answer 1

3

I've used the MaxMind GeoIP Lite database to good success before. Both city and country databases and APIs are available.

An example of usage is:

File dbfile = new File("db/GeoLiteCity.dat");
LookupService lookupService = new LookupService(dbfile, LookupService.GEOIP_MEMORY_CACHE);

Location location = lookupService.getLocation(ipAddress);

// Populate region. Note that regionName is a MaxMind class, not an instance variable
if (location != null) {
    location.region = regionName.regionNameByCode(location.countryCode, location.region);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Did this answer ever solve your problems, @KhAn SaAb? If so, it'd be great if it coudl be marked as accepted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.