, , ,

Recently, I was developing an application at my work place, that needed to deal with latitude and longitude information.

One of the features of the application was to plot the progress of an automobile on a map. Considering that we were testing the application on the Android Emulator, we faced a hurdle in that Google Maps is not very easy to access from the emulator. Additionally, we wanted to demonstrate the application even when access to Google Maps was not present, we decided to do the mapping ourselves.

To convert latitude and longitude information into corresponding screen coordinates, I did the most natural thing – did a brute force mapping into x and y. When that did not work, I tried to apply some tricks to the data. Finally, I went to down logical path – I did a Google search and found code on StackOverflow. The code implements Mercator Projection to convert latitude and longitude values to corresponding x and y values. While the code gave me the correct results, I realized that one of the reasons my brute force approach did not work is due to the convention.

While we are accustomed to write screen coordinates as X,Y, latitude and longitude values are exchanged. In other words, latitude values represent values on the Y-axis, while longitude values represent values on the X-axis. Non-intuitive? Quite so. It seems counter intuitive as longitude lines run vertically (left to right) and latitude lines run horizontally (top to bottom). To make it easy to remember, latitude lines cut across the Y-axis. Hence they have to be mapped to the Y-axis. Longitude lines cut across the X-axis. Hence they have to be mapped to the X-axis.

PS: The brute force method of mapping would never have worked, however much I try. If it was that simple, there would have been no need for a Mercator Projection routine at all 🙂

Link to Mercator Projection code — http://stackoverflow.com/questions/14329691/covert-latitude-longitude-point-to-a-pixels-x-y-on-mercator-projection