UK Geocoding and mapping in Drupal 7

Geocoding UK addresses has always seemed to be a pain especially outside of Drupal. With Drupal 7 a few of the mapping/location modules that I have been used to using in Drupal 6 are either not available as a stable release or don't quite do what I wanted. A key example is Location which I have used on a Drupal 6 with which geocoding has never worked (I still don't know if it is something that should work out the box, it certainly seems to imply to me.)

A rash of new Drupal 7 modules has made the process a lot easier though.

What you need

Required modules:

Required libraries:

Version numbers are as of writing

Enable all the modules including Geofield map which is used for displaying the map using Google Maps.

Module configuration

On the content type of your choice, create a new field with Field type set to Postal address. Within the settings you can define the country/countries of the address fields as well as which fields are shown within the widget.

Create another field, this time selecting Geofield as the Field type. Here you can select the field which will be used to geocode which will be the address field you set up before and which service you want to use for geocoding.

Assuming that you are having your nodes displayed the standard Drupal way (i.e. not overridden by Panels or Display Suite), go to Display Fields for you content type. Completely hide the address field (unless you want to display the address on the node).

Set the Format to Geofield map to display an actual map on the page for the geocode field. The additional settings allow you to control the zoom level of the map as well as whether users can pan or zoom around the map. Unfortunately there doesn't appear to be a way of selecting a different marker image which was possible in the Location module for Drupal 6.

Showing all locations on a map

As with most things in Drupal, the answer is Views.

Create a new view with the view type set to node/content displaying fields.

Set up your filters so that only the nodes you wish to be displayed are on the map. Then under fields, add in the geocode field. The Formatter you can play with but leaving it set to Well Known Text (WKT) should be fine (at least for this example).

You can also at this point set up another field which the contents will be shown within the popup infowindow within the Google Map.

Once you have your fields and filters sorted, under Format, select Geofield Map.

The first setting is Data Source. For this select your geocode field. You optionally also have the Popup Text option for the infowindow. All the other options should be self-explanatory.

The Views preview won't work due to the javascript, so you will need to save your view and go to the page you have created for it.

I have found that the zoom level within the Geofield Map view settings are ignored and the map will automatically zoom in/out to show all the markers.

And that's it. Full UK geocoding in a few minutes thanks to Drupal 7.

Comments

Thank you so much for this clear procedure, it worked first time.

I had spent hours fiddling with Openlayers before finding this post. addressfield, geofield and geocoder are the business!

roger@baileyitconsulting.ltd.uk

THANXXXX!