Today we’ll look at the Location module. Location has been around for as long as I can remember, in Drupal 6 it was one of the only ways to store address and geographical coordinates. In Drupal 7, it is just as powerful.
The module’s main purpose is to store addresses, but when you want to display the address in a map, then use the GMap module. Location comes tightly integrated with GMap, and most of the time you would use both modules together.
If you don’t want to use Google Maps via GMap, then you’ll need to integrate Location with other maps using custom code. If you want to use something other than Gmap, then look at IP Geolocation Views & Maps.
In this tutorial, I’ll show you how to store and geocode an address on an article using the Location module.
If you use Drush, simply run the following two commands:
$ drush dl location gmap views ctools
$ drush en location location_cck gmap gmap_location views_ui
How to Store Addresses
Location allows you to display its address form in two ways: through a field using the “Location CCK” module or directly in the node form using “Node Locations” module. The module’s data is always stored in the
location table no matter which sub-module you use. I prefer to define a custom field using “Location CCK”, but it’s just a preference.
In this tutorial, we’ll use the “Location CCK” module to display the address form.
1. Go to Structure, “Content types” and click on “manage fields” within the Article row.
2. Create a location field using the values defined in Table 1.0.
Table 1-0. Create location field
3. We need the module to allow us to collect City, “State/Province” and “Postal code” on the address form. On the “Field settings” page, select Allow from the Collect drop-down list for City, “State/Province” and “Postal code”.
Now that we have our field, let’s go ahead and configure Location to geocode any address entered.
How to Geocode an Address
Before we move on, let’s quickly discuss Geocoding.
Geocoding is a process of converting a postal address (street name, suburb) into geographical coordinates like latitude and longitude. Most of the mapping services out there like Google Maps offers geocoding.
To have addresses geocoded, all we need to do is configure Location to use a particular geocoding service for a specific country.
Update April 2017: A Google Maps API key is required.
1. Go to Configuration, Location and click on the “Geocoding Options” tab.
2. From the “Google Maps geocoding minimum accuracy” drop-down list select “Address level accuracy”.
3. For this tutorial, I’ll turn on geocoding just for Australia. But on a production site you would want to turn on geocoding for a few major countries.
Once you’ve selected a few countries, scroll to the bottom and click on “Save configuration”.
Test Article Location Field
Now that we have our field and geocoding setup, let’s go ahead and make sure addresses are getting converted.
1. Go to Content, “Add content” and click on Article.
2. Fill out the form and enter in a real address into the location field.
3. Once you save the form, you should see the address with coordinates.
Troubleshooting: How to debug Geocoding problems
So you followed along with this tutorial, added a location field and configured geocoding. But when you add an address it’s not being geocoded.
Here are four steps I would take to debug the issue.
1. Check the values in the location table
The first place to start your investigation is to look directly in the
location table in the database. Always look directly in the
location table and make sure there are values in the
2. Make sure you’re connected to the internet
Make sure you’re development computer or production server has access to the internet. For geocoding to work, Location needs to use those services over the internet.
3. Use a real address
This may seem obvious, but make sure you use a real address.
4. Can’t geocode address from a specific country
If you’re having trouble with addresses not being converted from a specific country; then make sure you configure geocoding for that country in the “Geocoding options” page.
As you can see, storing addresses in Drupal is pretty easy using Location. It only takes a few minutes to configure the module. In part two of this series, we’ll learn how to create a map using GMap and Views.
If you have any questions, please leave a comment.