Reverse Geocoding on ModestMaps
I have previously shown how to do reverse geocoding with MaxMind and now I want to show how to leverage a geocoding web service to visualize locations on a map. I have already done a ModestMaps with API calls and markers example in the past so I can leverage that to create this demo. Instead of using Google Maps Geocoding, I am going to use GeoNames Web Services and render the results on a ModestMap for comparison with MapBox image tiles.
To reverse geocode or lookup an address for a geographical location, you first need to provide some geo coordinates. I have created a simple ModestMaps map with geocoding on a double click event. I choose to listen to a double-click event on the map because I want the default zoom-in functionality to occur and I am printing the latitude and longitude values in marker on the map. This GeoCoding can now be used to query a web service and retrieve a location closest to the clicked point.
I added a simple XHR request function to make a request to GeoNames for the selected geo location. In the response, I parse the results, and send them to a markers function that draws a marker on the page. That function first clears all markers and I also clear markers on any map redraw. It turns out that a small zoom level on double click is not ideal, and I have made ModestMaps zoom a lot more on double click. But zooming in too far can also cause markers to be positioned outside of the bounds of the screen, so I check for this situation and when that happens, I zoom out and redraw the marker. Here is the reverse geocoding with ModestMaps and GeoNames. Double click anywhere on the map and compare cities in MapBox image tiles to the results from GeoNames.
Get to Grips with Slippy Maps
Modest Maps vs Processing
OpenStreetMap Nominatim Address Lookup