Address lookups with Leaflet and Nominatim ========================================== .. articleMetaData:: :Where: London, UK :Date: 2012-11-20 10:52 Europe/London :Tags: blog, php, openstreetmap :Short: addrlookup I recently wrote a patch_ for `joind.in`_ to add a map of an event's location to the event detail page. With the same patch, I also replaced the location part of the event edit page with a solution that uses JQuery_, Leaflet_ as map API, OpenStreetMap_ tiles_ and Nominatim_ for doing address lookups. This article forms a small tutorial on how to use this same set-up yourself. **The Basics** To start, we create a new directory for our project:: mkdir addresses cd addresses Then I downloaded the Leaflet and jQuery libraries and extracted them in the ``js`` directory of the project:: mkdir js curl -L https://github.com/CloudMade/Leaflet/zipball/v0.4.5 -o leaflet.zip unzip leaflet.zip mv CloudMade-Leaflet-*/dist/* js rm -rf CloudMade-Leaflet-* rm leaflet.zip curl http://code.jquery.com/jquery-1.8.2.min.js -o js/jquery-1.8.2.min.js As first step, we are simply going to show a map on a web page. The map is going to be full screen, and will not have any bells and whistles. The code to embed a map is small, but we will separate it into three files for clarity: a CSS file for our styles (``site.css``), an HTML file for the structure (``index.html``) and a JS file for all our JavaScript functions (``js/map.js``). Let's start with the HTML file::
', { html: "No results found" }).appendTo('#results'); } }); } This processes the results that came back from Nominatim. If there are results, we shows those including a ``Search results:`` header, and if there are no results, we show ``No results found``. Then we need to add one more function, the ``chooseAddr`` function which looks like:: function chooseAddr(lat, lng, type) { var location = new L.LatLng(lat, lng); map.panTo(location); if (type == 'city' || type == 'administrative') { map.setZoom(11); } else { map.setZoom(13); } } We simply use the latitude and longitude from the function invocation, and in order to make things slightly nicer we zoom in a bit less if the item type is either a ``city`` or an ``administrative`` border. As each of the returned results actually includes a full bounding box, we probably can use that to zoom in better, but I will leave that for your own experiments - you'd want the ``panInsideBounds()`` method of Leaflet's Map class for that. In the end, if we click on the ``Search`` button, a list is presented of all our search results: .. image:: images/address-3.jpg And after clicking one of the links, we see the map centered on Paris: .. image:: images/address-4.jpg The code for this example is available on github in my ``osm-tools`` repository at https://github.com/derickr/osm-tools/tree/master/leaflet-nominatim-example .. _patch: https://github.com/joindin/joind.in/pull/554 .. _`joind.in`: http://joind.in/ .. _jQuery: http://jquery.com .. _Leaflet_: http://leafletjs.com .. _OpenStreetMap: http://openstreetmap.org .. _tiles: http://wiki.openstreetmap.org/wiki/Tiles .. _Nominatim: http://nominatim.openstreetmap.org/ .. _here: http://wiki.openstreetmap.org/wiki/Nominatim#Parameters