In the good old Drupal 6 days, when you had to store an address and have that address converted into a latitude and longitude you would use the Location module. If you wanted to display a Google Map on your website, you would use the Gmap Module. In Drupal 7 the landscape has changed and now you have a lot options than just using the Location module.
A lot of the functionality that Location module gives you can be found in separate modules indicated in Table 1-0.
Table 1-0. Location based modules
|Address Field||Custom field that allows you to store addresses.|
|Geofield||Allows you to store geographic data in a number of formats.|
|GeoPHP||Provides Drupal integration with the geoPHP library.|
|Geocoder||Converts a postal address into a geographic data.|
In this article I’ll show you how to setup the modules mentioned above to allow website users to enter in an address and then have a map displaying the location of the address. You may think that installing four modules to handle addresses and locations is a bit too much. This method does give you some flexibility. Let’s say, for now, you only need to store addresses, but in the future you may want to store coordinates as well. Then, just setup Address Field to handle addresses for now and Geofield later.
Before we get started there are a few modules we need to download. Go download and install the following modules:
If you use Drush run the following command to download the required modules:
$ drush dl addressfield geophp geofield geocoder
Using Address Field and Geofield
Now that we have downloaded and installed all the required modules, let’s go ahead and setup our fields. For this article I have created a Venue content type which I’ll use throughout this article.
1. Go to Structure -> “Content types” (admin/structure/ types) and click the “manage fields” link on a specific content type. Create an Address Field using the settings defined in Table 1-1.
Table 1-1. Define an address field
|Field type||Postal address|
|Widget||Dynamic address form|
2. Click on the Save button to create the field, on the “Field settings” page click on the “Save field settings” button. Configure the address field if you need to, then click on “Save settings” to return to the “manage fields” page.
The field that we created above will be used to store the human readable postal address. The entered address could be displayed anywhere on the site. The next field we’ll create will store the coordinates for the entered postal address, we’ll use the Geofield module for this field.
3. Make sure you’re in the “manage fields” page and create a Geofield using the settings defined in Table 1-2.
Table 1-2. Define a Geofield field
|Widget||Geocode from another field|
If you don’t see the “Geocode from another field” widget, make sure you have downloaded and installed the Geocoder module.
4. Click on the Save button to create the field, on the “field settings” page click on the “Save field settings” button. Change the “Geocode from field” drop-down to Address.
5. Scroll to the bottom of the page and click on “Save settings”.
6. Now go a head and test everything. When you enter in an address the Location field should automatically be populated with coordinates.
At this point we have a content type called Venue where we store an address and coordinates. Currently the location field is just displaying coordinates, that’s a bit useless. Let’s display a map interface instead. Go to the Modules page and enable “Geofield map” module. This module – which ships with Geofield – simply offers a basic map interface.
1. Go to Structure -> “Content types” (admin/structure/ types) and click the “manage display” link on a specific content type. Change the Format for the Location field to “Geofield map”.
2. Configure the formatter if you have to, then click on Save.
3. Go back to your test content and you should see a map instead of coordinates.
It’s important to understand that there is always a few different ways to do location in Drupal and this is just one way. Do some research and don’t forget to review other modules.
Here’s a list of other modules:
If you have any questions please leave a comment.