I’ve talked about how to store addresses in Drupal before by using a combination of Address Field, Geofield, geoPHP and Geocoder.
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.
Getting Started
Before we begin, download Location, GMap, Views and Ctools. Then enable Location, “Location CCK”, GMap, “GMap Location” and “Views UI”.
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
Option | Value |
---|---|
Label | Location |
Machine name | field_location |
Field type | Location |
Widget | 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 latitude
and longitude
columns.
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.
“If you don’t want to use Google Maps via GMap, then you’ll need to integrate Location with other maps using custom code.”
Not so….
Check out IP Geolocation Views and Maps to combine any of the main 4 lat/lon providers with any of the big 3 mapping engines (Google, OpenLayers, Leaflet).
Thanks for the module, I have updated the tutorial.
Hi,
I followed the tutorial to store the address However I have the same Issue you listed : Issue 1 – Latitude & longitude values are 0 in the location table.And the map is not showing up because of this .If I submit the lat/long values mannually then it is getting stored & map is displaying .But On my site the address will be stored by users & lat/long will not be visible to them.
Your help is appreciated in resolving the issue.
Thanks & regards,
Leena S
Do you get the 0 value when you enter in an address? It sounds like you have a geocoding problem.
Double and triple check your geocoding options. Also, look at the “Troubleshooting: How to debug Geocoding problems” section.
Yes the value is 0.I tried with the address for Australia you have given in the sample still it does not store.
Try the Location module issue queue. I don’t know what the problem could be.
Yes lat/long values are 0 when i enter address.Is the google Map API key required for V3?
No, a key is not necessary.
https://developers.google.com/maps/signup?hl=en&csw=1
Update: API key required for Google Maps.
https://developers.google.com/maps/documentation/javascript/get-api-key
Hi,
I followed this tutorial and installed both gmap and location module. I created my custom content type Showroom in which added location as cck field. I want to create a location type view which list of all showrooms and also added exposed filters related to location like distance, country. I am facing issue in getting cck fields of content type showroom in views because my view type is location not content. How can I get cck field of showrrom content type in location type view? Is there any alternate way other than alter view query. If we create relationship how I can create. Your help is appreciated in resolving the issue.
Thanks…
I’ve never done what you are trying so I don’t know the exact answer. But I would use a relationship to get access to the node object in the view.
Also, ask in the Location issue queue and see if others have had the same problem.
Hi Iam using this module with location cck. If default country is not selected in location country field cck. Then during addition and edition of node Find address on Map button stops working. On click of that Error: m.change is not a function
m.change(‘locpickchange’);
Error is coming..
How I can resolve this error. Your help is appreciated in resolving the issue.
Sorry can’t help you. I would search the issue queue.
After doing a simple google search I found this issue, https://www.drupal.org/node/2174081
2017 – had to create a google developer api key for maps and place it into the config page. Heads up.
Thanks Mike.
I’ve added a link to https://developers.google.com/maps/documentation/javascript/get-api-key into the tutorial.
I do not want to use google maps or gmap, but I still want to geocode addresses. Will a combination of location, ip geolocation and leaflet work or there are other things as well?