Obtaining Geolocation in Play using IP Address

In this blog , I will explain how to obtain Geolocation in Play Scala application from IP address of user . You can easily fetch user’s current location or near by location by his IP address .

1) You will require API key to be able to use this feature. To get API key , just go to IPInfoDB .

2) Get user’s IP address

var ipAddress = request.remoteAddress

3) Provide the IPInfoDB’s API Key

val key = "Your API Key"

4) Then obtain the user’s location from the following link

val ipInfoDBURL = new URL("http://api.ipinfodb.com/v3/ip-city" + "/?key=" + key + "&ip=" + ipAddress + "&format=" + "json")
val conn = ipInfoDBURL.openConnection

5) At last get the data from the Input Stream of connection

val connection = ipInfoDBURL.openConnection
val input = new BufferedReader(new InputStreamReader(connection.getInputStream))
val geoLocation = new StringBuffer
for (i <- 0 to 12) {

will provide you Geolocation in JSON format with all details including Latitude and Longitude.

Note :- Add following line in application.conf file


Otherwise Play application wont be able to fetch Public IP Address of User.

This entry was posted in Agile, Play Framework, Scala, Web and tagged , , , . Bookmark the permalink.

4 Responses to Obtaining Geolocation in Play using IP Address

  1. somatik says:

    Better use the play WS instead of URL to avoid blocking

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s