December 2017: Please note that this post has not been updated for a while and there could be differences in terms of commands, screenshots and problems in running the code.

Google App Engine has announced version 1.6.5 of its App Engine SDK. I was pleasantly surprised to find an excellent feature that was hidden away in the Release Notes. Here is the nugget:

All user requests have X-AppEngine-Region, X-AppEngine-City, and X-AppEngine-CityLatLong headers which contain location information based on the IP address of the client request. For a full description of these headers, see:

https://developers.google.com/appengine/docs/java/runtime#Request_Headers

I had covered earlier on X-AppEngine-country and the fact that it worked quite well. And I had suggested just offhand that Google should probably take this one still further and provide other fine grained information too like city. It looks like they have gone further than that and include region, city and latitude / longitude.

In addition to the X-AppEngine-Country in the request header, it has now introduced the following additional location based headers with each request:

X-AppEngine-Region

X-AppEngine-City

X-AppEngine-CityLatLong

The availability of this information is surely going to be useful to a lot of applications that earlier had to resort to additional network calls to get this information from the IP Address.

To test out, I wrote a simple Google App Engine Java project which has a single page (index.jsp) that simply sniffs the value of these header and prints it out and shows a map of where you are visiting from.

Here is the snippet of code that prints out the different header values based where you are accessing the page:

<h2>Hello My Friend. Your co-ordinates are: </h2>

<h3> Country : <%=request.getHeader("X-AppEngine-Country") %></h3>

<h3> Region : <%=request.getHeader("X-AppEngine-Region") %></h3>

<h3> City : <%=request.getHeader("X-AppEngine-City") %></h3>

<h3> Latitude,Longitude : <%=request.getHeader("X-AppEngine-CityLatLong") %></h3>

Since I live in India, it does a good job and prints out the following (as shown in the screenshot below):

You can try it too.

Do you envision using this in your application? What have you been using so far to get similar functionality?

Source Code on GitHub.

Read more Episodes on App Engine Services