Convert IP Address to Geo location in Java
We will show you how you can convert IP Address to Geo Location. MaxMind provides a free Geo database and api. We will explain how you can leverage these to lookup the ip address. We provide some convenience methods that will allow you to lookup IP Address as a string or digit.
GeoIP Database
First lets get the database from MaxMind located here: GeoIP database download page
Project structure
+--src
| +--main
| +--java
| +--com
| +--memorynotfound
| +--geoip
| |--GeoIPv4.java
| |--GeoLocation.java
| |--LookUpProgram.java
| +--resources
| |--GeoLiteCity.dat
pom.xml
Maven Dependencies
<dependency>
<groupId>com.maxmind.geoip</groupId>
<artifactId>geoip-api</artifactId>
<version>1.2.14</version>
</dependency>
GeoLocation Model
Next lets start by creating a model for our GeoIP Location information.
package com.memorynotfound.geoip;
import com.maxmind.geoip.Location;
public class GeoLocation {
private String countryCode;
private String countryName;
private String postalCode;
private String city;
private String region;
private int areaCode;
private int dmaCode;
private int metroCode;
private float latitude;
private float longitude;
public GeoLocation(String countryCode, String countryName, String postalCode, String city, String region,
int areaCode, int dmaCode, int metroCode, float latitude, float longitude) {
this.countryCode = countryCode;
this.countryName = countryName;
this.postalCode = postalCode;
this.city = city;
this.region = region;
this.areaCode = areaCode;
this.dmaCode = dmaCode;
this.metroCode = metroCode;
this.latitude = latitude;
this.longitude = longitude;
}
// -- getters ommitted
public static GeoLocation map(Location loc){
return new GeoLocation(loc.countryCode, loc.countryName, loc.postalCode, loc.city, loc.region,
loc.area_code, loc.dma_code, loc.metro_code, loc.latitude, loc.longitude);
}
@Override
public String toString() {
return "GeoLocation{" +
"countryCode='" + countryCode + '\'' +
", countryName='" + countryName + '\'' +
", postalCode='" + postalCode + '\'' +
", city='" + city + '\'' +
", region='" + region + '\'' +
", areaCode=" + areaCode +
", dmaCode=" + dmaCode +
", metroCode=" + metroCode +
", latitude=" + latitude +
", longitude=" + longitude +
'}';
}
}
IP Address to Geo location
Now lets see how we can convert IP Address to Geo Location. We start by defining a LookupService
witch takes the location of the database and some options for caching reasons. We also add some convenience methods that will convert IP address to geo location using a string representation of the IP address or a digit representation.
package com.memorynotfound.geoip;
import com.maxmind.geoip.LookupService;
import java.io.IOException;
import java.net.InetAddress;
public class GeoIPv4 {
private static LookupService lookUp;
static {
try {
lookUp = new LookupService(
GeoIPv4.class.getResource("/GeoLiteCity.dat").getFile(),
LookupService.GEOIP_MEMORY_CACHE);
System.out.println("GeoIP Database loaded: " + lookUp.getDatabaseInfo());
} catch (IOException e) {
System.out.println("Could not load geo ip database: " + e.getMessage());
}
}
public static GeoLocation getLocation(String ipAddress) {
return GeoLocation.map(lookUp.getLocation(ipAddress));
}
public static GeoLocation getLocation(long ipAddress){
return GeoLocation.map(lookUp.getLocation(ipAddress));
}
public static GeoLocation getLocation(InetAddress ipAddress){
return GeoLocation.map(lookUp.getLocation(ipAddress));
}
}
Testing our GeoLocation lookup service
Finally lets test it. We provide a IP Address 77.229.28.185
which we know is used in NY City.
package com.memorynotfound.geoip;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class LookUpProgram {
public static void main(String... args) throws UnknownHostException {
long ipAddress = new BigInteger(InetAddress.getByName("72.229.28.185").getAddress()).longValue();
System.out.println("By String IP address: \n" +
GeoIPv4.getLocation("72.229.28.185"));
System.out.println("By long IP address: \n" +
GeoIPv4.getLocation(ipAddress));
System.out.println("By InetAddress IP address: \n" +
GeoIPv4.getLocation(InetAddress.getByName("72.229.28.185")));
}
}
Output
GeoIP Database loaded: GEO-533LITE 20141104 Build 1 Copyright (c) 2014 MaxMind Inc All Rights Reserved
By String IP address:
GeoLocation{countryCode='US', countryName='United States', postalCode='10003', city='New York', region='NY', areaCode=212, dmaCode=501, metroCode=501, latitude=40.731705, longitude=-73.9885}
By long IP address:
GeoLocation{countryCode='US', countryName='United States', postalCode='10003', city='New York', region='NY', areaCode=212, dmaCode=501, metroCode=501, latitude=40.731705, longitude=-73.9885}
By InetAddress IP address:
GeoLocation{countryCode='US', countryName='United States', postalCode='10003', city='New York', region='NY', areaCode=212, dmaCode=501, metroCode=501, latitude=40.731705, longitude=-73.9885}