Introducing OpenLayers Geocoder

Antonio De Marco
7 Apr 2010
14 Comments
Antonio De Marco
7 Apr 2010
14 Comments
Antonio De Marco, 7 Apr 2010 - 14 Comments

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:

{
  "status": "OK",
  "results": [ {
    "types": [ "point_of_interest", "establishment" ],
    "formatted_address": "Pizzeria da Vittorio di Martino Enzo, Via Benedetto Croce, 123, 00142 Rome, Italy",
    "address_components": [ {
      "long_name": "Pizzeria da Vittorio di Martino Enzo",
      "short_name": "Pizzeria da Vittorio di Martino Enzo",
      "types": [ "point_of_interest", "establishment" ]
    }, {
      "long_name": "123",
      "short_name": "123",
      "types": [ "street_number" ]
    }, {
      "long_name": "Via Benedetto Croce",
      "short_name": "Via Benedetto Croce",
      "types": [ "route" ]
    }, {
      "long_name": "Rome",
      "short_name": "Rome",
      "types": [ "locality", "political" ]
    }, {
      "long_name": "Rome",
      "short_name": "RM",
      "types": [ "administrative_area_level_2", "political" ]
    }, {
      "long_name": "Lazio",
      "short_name": "Lazio",
      "types": [ "administrative_area_level_1", "political" ]
    }, {
      "long_name": "Italy",
      "short_name": "IT",
      "types": [ "country", "political" ]
    }, {
      "long_name": "00142",
      "short_name": "00142",
      "types": [ "postal_code" ]
    } ],
    "geometry": {
      "location": {
        "lat": 41.8428020,
        "lng": 12.4858480
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 41.8344890,
          "lng": 12.4698406
        },
        "northeast": {
          "lat": 41.8511139,
          "lng": 12.5018554
        }
      }
    },
    "partial_match": true
  } ]
}

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.

ol_settings.png

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.

ol_searching.png

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.

ol_mapping.png

When we will be looking for a place, information like city, country, etc. will be automatically fetched into the selected text fields.

ol_filling.png

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.

Comments

Comments

ahura mazda
28 Jun 2010

I wonder if you know if we are legally allowed to use Google Geocoding services with maps other than Google Maps?

I've been thinking about this mash-up and found this: Google Maps API Terms of Service License Restrictions

nice, clear write-up, by the way.

I wonder if you know if we are legally allowed to use Google Geocoding services with maps other than Google Maps?

I've been thinking about this mash-up and found this: Google Maps API Terms of Service License Restrictions

nice, clear write-up, by the way.

ahura mazda, 28 Jun 2010

I wonder if you know if we are legally allowed to use Google Geocoding services with maps other than Google Maps?

I've been thinking about this mash-up and found this: Google Maps API Terms of Service License Restrictions

nice, clear write-up, by the way.

ahura mazda, 28 Jun 2010
NicolasH
11 Jul 2010

What a great solution! I haven't played with Google's geocoder for years, but is there a way to restrict the area it attempts to search in - and have that as an optional configuration for the widget?

So, if you know your data will only be relevant to Rome, don't try to look up locations on the rest of the planet.

What a great solution! I haven't played with Google's geocoder for years, but is there a way to restrict the area it attempts to search in - and have that as an optional configuration for the widget?

So, if you know your data will only be relevant to Rome, don't try to look up locations on the rest of the planet.

NicolasH, 11 Jul 2010

What a great solution! I haven't played with Google's geocoder for years, but is there a way to restrict the area it attempts to search in - and have that as an optional configuration for the widget?

So, if you know your data will only be relevant to Rome, don't try to look up locations on the rest of the planet.

NicolasH, 11 Jul 2010
schock
22 Oct 2010

This is awesome, we'll be using it to map stories over at http://vozmob.net. One thing that would be nice would be the ability for site admins to swap geocoding providers, just as they can swap map providers. So that we have other options besides google :) opengeo, nominatim, etc.

This is awesome, we'll be using it to map stories over at http://vozmob.net. One thing that would be nice would be the ability for site admins to swap geocoding providers, just as they can swap map providers. So that we have other options besides google :) opengeo, nominatim, etc.

schock, 22 Oct 2010

This is awesome, we'll be using it to map stories over at http://vozmob.net. One thing that would be nice would be the ability for site admins to swap geocoding providers, just as they can swap map providers. So that we have other options besides google :) opengeo, nominatim, etc.

schock, 22 Oct 2010
Anonymous
13 Nov 2010

Very nice, thank you. But when i enable this module, I get an error on the create content page which states:

warning: Parameter 1 to openlayers_cck_widget() expected to be a reference, value given in /Library/WebServer/Documents/UrbanPins/includes/module.inc on line 462.

Any thoughts?

Very nice, thank you. But when i enable this module, I get an error on the create content page which states:

warning: Parameter 1 to openlayers_cck_widget() expected to be a reference, value given in /Library/WebServer/Documents/UrbanPins/includes/module.inc on line 462.

Any thoughts?

Anonymous, 13 Nov 2010

Very nice, thank you. But when i enable this module, I get an error on the create content page which states:

warning: Parameter 1 to openlayers_cck_widget() expected to be a reference, value given in /Library/WebServer/Documents/UrbanPins/includes/module.inc on line 462.

Any thoughts?

Anonymous, 13 Nov 2010
Joshua Needham
22 Nov 2010

I've been reading into this a lot lately and it's related to a PHP 5.3 issue and CCK among other things. There is a patch for the CCK module however it didn't fix for me so I'm going to attempt setup on my server which is 5.2.14 and hopefully it will solve the error.

I've been reading into this a lot lately and it's related to a PHP 5.3 issue and CCK among other things. There is a patch for the CCK module however it didn't fix for me so I'm going to attempt setup on my server which is 5.2.14 and hopefully it will solve the error.

Joshua Needham, 22 Nov 2010

I've been reading into this a lot lately and it's related to a PHP 5.3 issue and CCK among other things. There is a patch for the CCK module however it didn't fix for me so I'm going to attempt setup on my server which is 5.2.14 and hopefully it will solve the error.

Joshua Needham, 22 Nov 2010
Joshua Needham
27 Dec 2010

Moving from my local dev environment to my web server did fix the issue. Definitely looking forward to the reverse geocoding functionality.

Moving from my local dev environment to my web server did fix the issue. Definitely looking forward to the reverse geocoding functionality.

Joshua Needham, 27 Dec 2010

Moving from my local dev environment to my web server did fix the issue. Definitely looking forward to the reverse geocoding functionality.

Joshua Needham, 27 Dec 2010
Quentin
26 Dec 2010

Many thanks for this!

Just also wanted to let you know that it's really the reverse geocoding I'm after, so looking forward to that...

Many thanks for this!

Just also wanted to let you know that it's really the reverse geocoding I'm after, so looking forward to that...

Quentin, 26 Dec 2010

Many thanks for this!

Just also wanted to let you know that it's really the reverse geocoding I'm after, so looking forward to that...

Quentin, 26 Dec 2010
Anonymous
25 Jan 2011

It seems that Google Geocoding Web Service will perfectly work for our current project. Thanks for the overview.

It seems that Google Geocoding Web Service will perfectly work for our current project. Thanks for the overview.

Anonymous, 25 Jan 2011

It seems that Google Geocoding Web Service will perfectly work for our current project. Thanks for the overview.

Anonymous, 25 Jan 2011
Stefan
25 Mar 2011

Great post! Thank you. I hope it will be avaiable for Drupal 7 soon.

Great post! Thank you. I hope it will be avaiable for Drupal 7 soon.

Stefan, 25 Mar 2011

Great post! Thank you. I hope it will be avaiable for Drupal 7 soon.

Stefan, 25 Mar 2011
Mario
13 May 2011

Thanks for this great post. It would be perfect for a project that I have in mind. Do you know if there will be a version for Drupal 7?

Thanks for this great post. It would be perfect for a project that I have in mind. Do you know if there will be a version for Drupal 7?

Mario, 13 May 2011

Thanks for this great post. It would be perfect for a project that I have in mind. Do you know if there will be a version for Drupal 7?

Mario, 13 May 2011
Admin
30 May 2011

We had a look at porting to Drupal 7 as requested by previous posters: the porting requires significant work because of infrastructural changes, but it is feasible. If someone is interested in sponsoring a Drupal 7 port we can talk about it, just let us know.

We had a look at porting to Drupal 7 as requested by previous posters: the porting requires significant work because of infrastructural changes, but it is feasible. If someone is interested in sponsoring a Drupal 7 port we can talk about it, just let us know.

Admin, 30 May 2011

We had a look at porting to Drupal 7 as requested by previous posters: the porting requires significant work because of infrastructural changes, but it is feasible. If someone is interested in sponsoring a Drupal 7 port we can talk about it, just let us know.

Admin, 30 May 2011
Unique
4 Oct 2011

Amazing stuff ;) Just waiting for it to come out on Drupal 7 so I can use it on my site. Can't wait! Looking forward to it

Amazing stuff ;) Just waiting for it to come out on Drupal 7 so I can use it on my site. Can't wait! Looking forward to it

Unique, 4 Oct 2011

Amazing stuff ;) Just waiting for it to come out on Drupal 7 so I can use it on my site. Can't wait! Looking forward to it

Unique, 4 Oct 2011
Anonymous
3 Nov 2011

Sometimes don't retrieve results, and sometimes yes. My maps are openstreetmaps, no google maps. Maybe is this the reason?

Sometimes don't retrieve results, and sometimes yes. My maps are openstreetmaps, no google maps. Maybe is this the reason?

Anonymous, 3 Nov 2011

Sometimes don't retrieve results, and sometimes yes. My maps are openstreetmaps, no google maps. Maybe is this the reason?

Anonymous, 3 Nov 2011
Andrea Pescetti
4 Nov 2011

Comments are closed. Please use the issue queue on Drupal.org for reporting bugs, and if you need custom development on OpenLayers Geocoder feel free to contact us through the contact form on this site.

Comments are closed. Please use the issue queue on Drupal.org for reporting bugs, and if you need custom development on OpenLayers Geocoder feel free to contact us through the contact form on this site.

Andrea Pescetti, 4 Nov 2011

Comments are closed. Please use the issue queue on Drupal.org for reporting bugs, and if you need custom development on OpenLayers Geocoder feel free to contact us through the contact form on this site.

Andrea Pescetti, 4 Nov 2011

Get your project started today!

Contact us