Java – find location using Ip Address

Geolocation

In this example, we show you how to find a location (country, city, latitude, longitude) using an IP address.

1. GeoLite Database

The MaxMind provides a free GeoLite database (IP Address to Location).

  1. Get a free GeoLite free Databases – here
  2. Get a GeoIP client Java APIs – here
  3. Start code it.

2. GeoLite Java Example

An example to use GeoIP client Java APIs to find a location using IP address.

GetLocationExample.java

package com.mkyong.analysis.location;

import java.io.File;
import java.io.IOException;
import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import com.maxmind.geoip.regionName;
import com.mkyong.analysis.location.mode.ServerLocation;

public class GetLocationExample {

  public static void main(String[] args) {
	GetLocationExample obj = new GetLocationExample();
	ServerLocation location = obj.getLocation("206.190.36.45");
	System.out.println(location);
  }

  public ServerLocation getLocation(String ipAddress) {

	File file = new File(
	    "C:\\resources\\location\\GeoLiteCity.dat");
	return getLocation(ipAddress, file);

  }

  public ServerLocation getLocation(String ipAddress, File file) {

	ServerLocation serverLocation = null;

	try {

	serverLocation = new ServerLocation();

	LookupService lookup = new LookupService(file,LookupService.GEOIP_MEMORY_CACHE);
	Location locationServices = lookup.getLocation(ipAddress);

	serverLocation.setCountryCode(locationServices.countryCode);
	serverLocation.setCountryName(locationServices.countryName);
	serverLocation.setRegion(locationServices.region);
	serverLocation.setRegionName(regionName.regionNameByCode(
             locationServices.countryCode, locationServices.region));
	serverLocation.setCity(locationServices.city);
	serverLocation.setPostalCode(locationServices.postalCode);
	serverLocation.setLatitude(String.valueOf(locationServices.latitude));
	serverLocation.setLongitude(String.valueOf(locationServices.longitude));

	} catch (IOException e) {
		System.err.println(e.getMessage());
	}

	return serverLocation;

  }
}

Output


ServerLocation [countryCode=US, countryName=United States, region=CA, 
	regionName=California, city=Sunnyvale, postalCode=94089, 
	latitude=37.424896, longitude=-122.0074]

References

  1. Wikipedia : Geolocation software
  2. GeoLite Free Downloadable Databases

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
31 Comment threads
10 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
32 Comment authors
m taRamiz Yaseensajjad haiderdeepakSaurabh Recent comment authors
newest oldest most voted
TJSE
Guest
TJSE

Can anyone provide link to get jar to get import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import com.maxmind.geoip.regionName; classes

Basant Kumar
Guest
Basant Kumar

Hi , I am getting null pointer exception while using another IP . My Code: try { GetLocationExample obj = new GetLocationExample(); ServerLocation location = obj.getLocation(“192.168.1.105”); System.out.println(location); Class :ServerLocation } catch (Exception ex) { ex.printStackTrace(); } Class : GetLocationExample package com.action; import java.io.File; import java.io.IOException; import com.maxmind.geoip.Location; import com.maxmind.geoip.LookupService; import com.maxmind.geoip.regionName; import com.action.ServerLocation; public class GetLocationExample { public ServerLocation getLocation(String ipAddress) { File file = new File( “C:/resources/location/GeoLiteCity.dat”); return getLocation(ipAddress, file); } public ServerLocation getLocation(String ipAddress, File file) { ServerLocation serverLocation = null; try { serverLocation = new ServerLocation(); LookupService lookup = new LookupService(file,LookupService.GEOIP_MEMORY_CACHE); Location locationServices = lookup.getLocation(ipAddress); serverLocation.setCountryCode(locationServices.countryCode); serverLocation.setCountryName(locationServices.countryName);… Read more »

Ankur Sandilya
Guest
Ankur Sandilya

Hi , Do i need to download the GeoLite database to implement this in my application . Is there a process to include this in your desktop application ?

Joe
Guest
Joe

Please add a download link to get this example project

SAJID
Guest
SAJID

com.mkyong.analysis.location.mode.ServerLocation what is jar file this?/

Souleh Shaikh
Guest
Souleh Shaikh

Try this code This is what you are looking for, it is a class file and not contain in any jar file. package com.mkyong.analysis.location.mode; /** * ServerLocation */ public class ServerLocation { private String countryCode; private String countryName; private String region; private String regionName; private String city; private String postalCode; private String latitude; private String longitude; @Override public String toString() { return city + ” ” + postalCode + “, ” + regionName + ” (” + region + “), ” + countryName + ” (” + countryCode + “) ” + latitude + “,” + longitude; } public String… Read more »

sajjad haider
Guest
sajjad haider

GeoLiteCity.dat I need this file , i cannot fine this file in .dat format please send me in my email. i shall be thankfull to you.

deepak
Guest
deepak

where is this file
import com.mkyong.analysis.location.mode.ServerLocation;

Srijini
Guest
Srijini

hi, i did the code by downloading both jar file and dat file. but on running i got error as:

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 31162416

at com.maxmind.geoip.LookupService.seekCountry(LookupService.java:851)

at com.maxmind.geoip.LookupService.getLocation(LookupService.java:709)

at com.maxmind.geoip.LookupService.getLocation(LookupService.java:534)

at com.maxmind.geoip.LookupService.getLocation(LookupService.java:548)

.please give me a suggession . thanks

Shivani
Guest
Shivani

Use GeoLiteCity.dat database instead of the one for the country

Justin James
Guest
Justin James

Thank you..

Ramiz Yaseen
Guest
Ramiz Yaseen

ipgeolocation.io is another IP geolocation API service that sounds a good alternative to Maxmind geoip location service.
You can also use IP geolocation API Java SDK. In the following link, you will go through the basic steps to use IP Geolocation API Java SDK.
Link: https://ipgeolocation.io/documentation/ip-geolocation-api-java-sdk-20180807094025

Saurabh
Guest
Saurabh

import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import com.maxmind.geoip.regionName;

These 3 classes are not available in Jar ??

TJSE
Guest
TJSE

I am getting error for below imports
import com.maxmind.geoip.Location;
import com.maxmind.geoip.LookupService;
import com.maxmind.geoip.regionName; classes

oum rouma
Guest
oum rouma

Hi
im getting LookupService Class error
please give me a suggession
thanks

Igor Bajovic
Guest
Igor Bajovic

Where we can download fresh new GeoLiteCity.dat? Is it maintained by someone?

Parth Prajapati
Guest
Parth Prajapati

It is very helpful blog.It is Working for me.
I want to know If there is another database that i can use which is more accurate then this then it will be very grateful of you.
Thanks.

burhan
Guest
burhan

File file = new File(
“C:\resources\location\GeoLiteCity.dat”);
How to include the file if i have hosted the file in amazon S3?
I tried with uri and url.getpath() etc. put it didn’t work. Any guesses?

ashutosh
Guest
ashutosh

import com.mkyong.analysis.location.mode.ServerLocation; please provide jar file

phat
Guest
phat

How can you have the library com.mkyong.analysis.location.mode.ServerLocation;?

fsdfsd
Guest
fsdfsd

?????? ??????

francis
Guest
francis

locationServices.region and locationServices.city occurs null pointer exception.

franko
Guest
franko

locationServices.city and locationServices.region occurs null pointer exception.

Jandi
Guest
Jandi

wonderful website. This website cleared by my all doubt and simply understand than the other website. and

Stasio Mod
Guest
Stasio Mod

MKYong, quck note that you have a grammatical mistake.
Start code it should be Start coding or Start coding it

Vyshak
Guest
Vyshak

Stasio Mod.. Spell check.. It’s “quick” ,not “quck” ..lol.. :)

som
Guest
som

import com.mkyong.analysis.location.mode.ServerLocation;???? where is the file???

Azim
Guest
m ta
Guest
m ta

404 not found!!!!!!!!!!!!!

snow
Guest
snow

locationServices.countryCode occurs null pointer exception.

snow
Guest
snow

what should I write at C:\resources\location\GeoLiteCity.dat file?

I’m getting
java.io.IOException: Negative seek offset

error.

udaykiranpulipati
Guest
udaykiranpulipati

Use below class if you got error package com.testipaddress.sitereference; public class ServerLocation { public void setCountryCode(String countryCode) { System.out.println(“CountryCode:”+countryCode); } public void setCountryName(String countryName) { System.out.println(“CountryName:”+countryName); } public void setRegion(String region) { System.out.println(“Region:”+region); } public void setRegionName(String regionNameByCode) { System.out.println(“RegionName:”+regionNameByCode); } public void setCity(String city) { System.out.println(“City:”+city); } public void setPostalCode(String postalCode) { System.out.println(“PostalCode:”+postalCode); } public void setLatitude(String valueOf) { System.out.println(“Latitude:”+valueOf); } public void setLongitude(String valueOf) { System.out.println(“Longitude:”+valueOf); } }

sachin
Guest
sachin

same error i m having..could locate serverlocation

sachin
Guest
sachin

Thanks a lott…very good example. ServerLocation is just a Simple Java class Venky with class members CountryCode,CountryName,PostalCode etc…

venky
Guest
venky

im getting serverLocation Class file error how i fix,
The above
1. Get a free GeoLite free Databases – here
2.Get a GeoIP client Java APIs – here

which downloads i have to download and configure

a)for this “import com.maxmind.geoip” i added jar file this import no errors
b)serverLocation class file only problem how i fix and which file confiugure i missed
in the above site so many downloads are there

Azim
Guest
trackback
Spring MVC – find location using IP Address (jQuery + Google Map)

[…] Java – Find location using IP address […]

MAP
Guest
MAP

Hi,

I’m trying to run this code using GeoLiteCity.dat and the dependency jars but I’m getting a null pointer exception.

Can anyone explain why this is happenning so that I can correct or even better explain how the location is being retrieved.

Thanks in advance

Basant Kumar
Guest
Basant Kumar

My Code: try { GetLocationExample obj = new GetLocationExample(); ServerLocation location = obj.getLocation(“192.168.1.105″); System.out.println(location); } catch (Exception ex) { ex.printStackTrace(); } ServerLocation Class: package com.action; public class ServerLocation { private String countryCode; private String countryName; private String region; private String regionName; private String city; private String postalCode; private String latitude; private String longitude; @Override public String toString() { return city + ” ” + postalCode + “, ” + regionName + ” (” + region + “), ” + countryName + ” (” + countryCode + “) ” + latitude + “,” + longitude; } public String getCity() { return… Read more »

sajjad haider
Guest
sajjad haider

GeoLiteCity.dat I need this file , i cannot fine this file in .dat format please send me in my email. i shall be thankfull to you.