Introducing OpenLayers Geocoder
Drag-and-zoom free location spotting
The OpenLayers suite represents a breakthrough in the Drupal mapping solutions. The module allows to combine maps coming from different providers (Google Maps, Yahoo, and many others...) and, by using services like CloudMade or MapBox, it is finally possible to have maps perfectly integrated into the website look and feel. In order to input geospatial information using OpenLayers we have to enable the OpenLayers CCK module: the input widget provides a map where users can easily mark the desired location.
Unfortunately, this approach does not scale to deal with more complex scenarios. Imagine a website where users need to geo-locate restaurants in different cities: the input process will result in a series of very tedious drag-and-zoom operations. Ideally, the only information the user would need to provide is the address or the name of the location he or she is looking for.
Google Geocoding Web Service
The Google Geocoding Web Service V3 is a very powerful service that accepts full street addresses, location names and even known places (i.e. airports, train stations etc.), returning a complete set of information about the location we are looking for. For instance, Let's have a closer look to the web service response for Pizzeria da Vittorio, Rome:
http://maps.google.com/maps/api/geocode/json?address=Pizzeria+Da+Vittorio,+Rome&sensor=false
results in:
{<br> "status": "OK",<br> "results": [ {<br> "types": [ "point_of_interest", "establishment" ],<br> "formatted_address": "Pizzeria da Vittorio di Martino Enzo, Via Benedetto Croce, 123, 00142 Rome, Italy",<br> "address_components": [ {<br> "long_name": "Pizzeria da Vittorio di Martino Enzo",<br> "short_name": "Pizzeria da Vittorio di Martino Enzo",<br> "types": [ "point_of_interest", "establishment" ]<br> }, {<br> "long_name": "123",<br> "short_name": "123",<br> "types": [ "street_number" ]<br> }, {<br> "long_name": "Via Benedetto Croce",<br> "short_name": "Via Benedetto Croce",<br> "types": [ "route" ]<br> }, {<br> "long_name": "Rome",<br> "short_name": "Rome",<br> "types": [ "locality", "political" ]<br> }, {<br> "long_name": "Rome",<br> "short_name": "RM",<br> "types": [ "administrative_area_level_2", "political" ]<br> }, {<br> "long_name": "Lazio",<br> "short_name": "Lazio",<br> "types": [ "administrative_area_level_1", "political" ]<br> }, {<br> "long_name": "Italy",<br> "short_name": "IT",<br> "types": [ "country", "political" ]<br> }, {<br> "long_name": "00142",<br> "short_name": "00142",<br> "types": [ "postal_code" ]<br> } ],<br> "geometry": {<br> "location": {<br> "lat": 41.8428020,<br> "lng": 12.4858480<br> },<br> "location_type": "APPROXIMATE",<br> "viewport": {<br> "southwest": {<br> "lat": 41.8344890,<br> "lng": 12.4698406<br> },<br> "northeast": {<br> "lat": 41.8511139,<br> "lng": 12.5018554<br> }<br> }<br> },<br> "partial_match": true<br> } ]<br>}
As you can see the web service tells us pretty much everything we want to know about the location. It's time to use all this awesomeness to make the user's life shining again!
Meet OpenLayers Geocoder
OpenLayers Geocoder provides a new input widget for OpenLayers CCK fields that makes location spotting a fast and painless experience. All we need to do is to select the OpenLayers Geocoder input widget from within the CCK field setting page.
After enabling the OpenLayers Geocoder widget, adding a restaurant is all about providing its name or address: OpenLayers Geocoder will provide the user with a list of possible locations.
Get the most out of the web service response
We have only used the geospatial information from the web service response so far: latitude and longitude to center the map and the boundary box to nicely fit the desired location. Besides that, the response contains really valuable information about the location we were looking for, like postal code, administrative area, city, country, etc.
OpenLayers Geocoder gives the possibility to fill CCK text fields automatically on the node submission form with data coming from the response object. This is possible thanks to the integration with the Token module: all address parts are exposed and ready to be used as replacement patterns. To enable the autofilling we have to visit the OpenLayers CCK field setting page in order to map which token is going to fill which text field.
When we will be looking for a place, information like city, country, etc. will be automatically fetched into the selected text fields.
This gives us a great flexibility: we can display our nodes by city using Views or search through them using Faceted Search.
Future development: Reverse geocoding
Another very interesting feature of the Google Geocoding Web Service is the possibility to perform reverse geocoding. As the name suggests, this time the user will spot a location on the map and the web service will return the closest address to the given point. OpenLayers Geocoder will have support for reverse geocoding in one its next releases.
Tags: Drupal PlanetMapping tools