Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When it comes to entering addresses in Drupal, you could of course just setup a bunch of CCK fields such as street, city, state and so on, then tie those CCK fields into a free geocoder, such as the one provided by Google's API and it would work. Fortunately though, you don't have to roll your own solution. Address entry in geocoding is such a common need that a Drupal developer named Brandon Bergren put together some modules that do it all for you. The first one we're going to look at is the location module, which you can get from drupal.org/project/location. Brandon also wrote the GMap module. We'll show you how to use that module in the video mapping with the GMap module.
So you'll see how a lot of hooks between the two modules allow them to operate in tendon. I should warn you ahead of time that this module isn't the most polished. You'll notice that in the demonstration ahead. But as with many things in Drupal, it's continually involving. So there is hope for improvement. Also the competitor module called GO holds a lot of promise, and might eventually supplant the location module. Right now though, the Location Module is the most popular and mature one out there that does what it does. But do check back, as they say in the Drupal community, the drop is always moving. The first thing that you need to do is download and install the Location Module.
Once you have it in your Drupal folder, go to Administer > Site building and Modules, then scroll down until you see the Location group and there it is. Location installs many modules; the only one you need to turn on at first is the one called Location. We'll discuss some of the other modules later in this video and also in the mapping with the GMap module video. But for now, we'll just go down to the bottom of the page, click Save configuration to turn it on. Once you've enabled that module, go to your Administer page, the configuration page for the location module is found in the Setting section under Site configuration, go down to Location and click on it.
There are three parts to configuring the location module. Let's look at the first one. That's Main settings. There actually aren't very many settings on this page; in fact, we don't need to change any of them. If you wanted to allow people to set their latitude and longitude by simply clicking on a map, then you'd enable this checkbox. As for the other settings there is plenty of explanatory text there should you want to explore them, but for us we'll just leave it as it is. The second tab here Map links, gives a list of the different services you can use with the location module. As we scroll down, you see it's very often Google, Yahoo! and MapQuest. Google Maps is always the one that's turned on by default so we'll also leave this turned on the way that it is just fine.
We go back to our Geocoding options page. I'll turn off Albania and only go down to the United States, since that's the only country that I'm going to be mapping. The fastest way to find that is to hit Command+F or Ctrl+F on the PC then type in, United States. Once down there, scroll down a little bit, so you can see all your choices and we'll turn on Google Maps. Scroll to the end, and save our configuration. I recommend that you do these steps before you even start entering addresses. It is possible to enter addresses first and then geocode them later but the location module isn't very well setup for that. So it's best to take care of the Google Maps stuff right upfront.
The next thing that you need to do is sign up for a Google Maps API key. That's how that Google Maps knows who exactly is getting their geocoding information. Unfortunately, it's not that easy to find in the location module but you can find it if you go back to the Main Settings tab. Look down here where they talk about having a Google Maps API key. Click on that. It takes you to the Google Maps API page. From there you will see a link somewhere that says Sign up for Google Maps API key. Once again you see a long list of terms and conditions, at the bottom if you agree, you click this checkbox, and type in your URL. In our case that's simply local host. Although of course, if you do create your site locally and then put it on a server, you will need to get a new Google Maps API key otherwise your maps won't work.
Don't worry though. They're all free. I type in localhost and click on Generate API Key. If you haven't already created a Google account, you'll have to do so here. I have, so I'm going to enter it and then get my key. Once you're signed into Google, you're given your key, simply copy it by hitting Command+C on the Mac or Ctrl+C on the PC, and go back to your Drupal installation, I'll do that by hitting the Back button a few times. Once you have your gmap key, go back to Geocoding options and search once again for United States or whatever other countries you have enabled. After you had saved it, the last time you can now configure parameters including entering that API key.
Now, we just need to paste that key into our field. On the Mac, you use Command+V. On the PC you use Ctrl+V and click Save configuration. That's it for the initial setup. Now, let's start geocoding. What we're going to do is we're going to create a household for each person to live in. we're making the assumption that each person has only one household but that each household can contain many people and what's called a one to many structure. We could do it in other ways. For example, we could allow people to have more than household or we could force each person to enter their own household address and just forget about relating it altogether. Like all decisions, it's a tradeoff.
I'm sure you can see how these tradeoffs can have strong social and even political ramifications. So the way you plan your data structure really does mean a lot. But let's go ahead and create our content type. As you know we do that by going to Content management > Content types and Add content type. We'll call this content type Household. The machine name will be household with a lower case h. The Description is let's say 'A place where people and pets live.' Scroll down a little bit.
For our Submission forms settings, we'll be doing something interesting with the title in a minute. But I just want to change this Body field label. So it's Notes about this household. Just in case we want to keep such notes. We don't really need any submission guidelines. Scrolling down little further, let's make it so that it's not promoted to the front page. Technically that won't make a difference in our site because you remember our front page is actually that table showing all of the people. But we might as well keep it clean. Scrolling down further, we see this new area that we hadn't seen before, Locative information. This is where you actually specify how many locations and how they're displayed and so forth. The first setting is Number of locations.
Here the module does something strange by default. It says that you're going to have zero locations, but then you're going to add them three at a time. Well, that doesn't make any sense and it doesn't matter to us because we're just going to have one location, easy enough to change. Change it to 1 location minimum, 1 location maximum and 1 added at a time. Let's take a look at some of the other settings. For Collection settings, again the defaults are a little bit strange. The programmer for some reason did not have people entering their City, State or Postal code but we actually do want to allow people to do that, so we'll change that to Allow.
Incidentally, you can also force a default. So for example, since we're only working with United States, we could force default to United States. But I'm just going to leave it as it is. It doesn't really matter that much. If someone enters an address outside of the United States, it simply won't be mapped but it will still be on the database. There is another section here I want to make sure you notice, Collapsible and Collapsed. Again, a sort of strange default is that when somebody fills out this form, the location information doesn't show up. It shows up as one of those little tiny clickable areas like you see here and very often people filling out the form will miss it. My preference for location is to get rid of both Collapsible and Collapsed. That way they're forced to see the location and enter it.
Scrolling down further, we can change RSS Settings. We won't be getting into that. It's actually a fairly arcane subject, you can leave through the options if you like and read up about them on drupal.org. For Display Settings you have the option of hiding certain pieces of information. You can see which one of those are here. I'm just going to leave them all showing, so we can see what's going on. Finally, of course, you can allow people to put comments on locations, which we won't do. There's one final thing I want to do to this content type, all that I really wanted for is for people to enter an address. They don't have to enter a title for the address or anything else. I'm going to scroll up to the top and using a trick that you remember from earlier in the course. I'm going to give it an Automatic title. This is using the Auto Nodetitle module. I'll automatically generate the title and then look at my Replacement patterns.
When you installed the location module, it also installed a number of these new tokens. If you scroll down, you will see all of these new Location tokens. They all have this unusual thing at the end, _N that's because with locations, you remember you can have multiple locations in a node and the N value is what you used to say which location you want. If you wanted to point to the first location, you'd say location 0 because of the way computers count 0 is actually the first one. If you wanted the second one, it would be 1 and so forth. Well I'm not going to go through all of that right now. I happen to know exactly what sort of replacement pattern I want to use. It's going to be [location-street_0 ], [location-city_0], and then we could scroll down to the bottom and save our new content type, all the way down.
We're really getting quite fast at creating these content types and now, we're even ready to go and create our first household. We do that as usual by going to Create content. We see Household and there is our form. As expected, we don't actually see any title up here because it's being automatically created. There is our Notes about this household and as we scroll down further, this is where we actually enter the information. I'm going to call this house, House of Pretty Good Repute, the Street is 1 South Main Street, the City is Oberlin, Ohio, and the Postal code is 44074.
By the way if you get any bright ideas of going to that address, it's just a bank. Don't worry about it. Let's scroll to the bottom and click Save, and we have created it. Now, you we'll notice something that we didn't enter but that Google actually gave us, it's these, the coordinates. That's the geocoding magic. But I do want to mention something. If you can't get an address to geocode properly because the geocoder is getting information from a remote server, it only works if you are online. Also geocoding will only do as much as it can. In the Google maps API for example, it will figure out a zip code if you don't provide one. It'll geocode to the middle of a city, if you don't give the address.
But if you don't give a city and it doesn't really know where 1 Main Street is, it has a choice of many, many cities, obviously it won't be able to give you the coordinates. So we now have households, but we still don't have anyone living in them. Let's fix that now by adding a node reference field to the person's content type. What we're going to do is make it so that each person is referred to a household. To do so, we'll go back to our household content type. By clicking Administer > Content management and Content types. Then of course we go back to our Household and manage fields.
The New field we are going to add is called Resident, field name is resident, and the field type is Node reference. I think I want to show people a whole list of checkboxes so that they could check exactly which people are in the household. Mind you if you start dealing with hundreds and hundreds of people, you'll probably want to use something else such is an auto complete field. But for our purposes since we only have about a dozen or so people, I'm going to go with that. Scroll down to the bottom and save, which takes us to our settings page. The only thing I'm going to change here is the Number of values. I want to allow any number of people to be living in this household. So I'll change that to Unlimited. What exactly goes into the household, a person, so that's the type that gets referenced here. Scroll down to the bottom and Save. Now we've households and we have people and we have a way to connect the two.
Let's go ahead and do that. We'll do that by going to Administer > Content management and Content and find the household that we just created, 1 South Main Street. Remember that title was automatically created from the address that we put in. We'll go and edit it, and as we scroll down, we see a list of all the people who can live there. I'm going to put Tom de Nada in that house, scroll down and save. Now, if we go back to that household, we actually see the residents who live there. That information continues to be referenced in other ways so that we can build a view for example that shows everybody who lives in every household and so forth. We could add more people, we can remove more people, we can click to go to Tom's node if we want and so forth.
So we've completely done exactly what we wanted to and if we want to, we can go back through and put all of the people into households as desired. In fact, if you go into your exercise files that's exactly what we've done. All you need to do is load up the exercise files and you'll see that people are now assigned to households. You might remember when we first installed it there were quite a lot of modules installed. I'm going to go back and just talk a little bit about what those modules are very briefly. I would recommend though that you spend a little bit of time if you start doing any sort of geo location Drupal sites and actually get to know what all of the location modules are.
To go and look at them, we go to Administer > Site building and Modules and I'm going to start scrolling down. Do you remember that there was a location group at the end of the screen, but also when you installed location it put something in the CCK group called Location CCK? So that's another way you can add locations, not just in the core of a node but as a CCK field. Scrolling down further we see all of these different ones. GMap, we'll be discussing when we show you how to map the different locations in Google Maps. Location Add Another lets you add multiple locations one after another in an interesting interface. Location Fax, Location Phone, these are simply fields that you add on to location. I personally have never found them very useful but maybe you will.
User Locations lets you assign locations to each individual member of your Drupal site and then later it shows you a map, showing where all of your members live, assuming that they entered their location. I find that very interesting, you'll see that particular functionality on a lot of sites. Finally, Location Search gives you some interesting options for example, for proximity searches, who is nearby, who is within 100 miles and so forth. Finally, I just want to mention one other thing. If you search this page for Locale, you see that there is actually a module in core called Locale. That causes a lot of confusion, because it's related to translations not location and on each user's account page, there is a section called Locale Setting that has to do with time zones. So just remember, when you're dealing with physical places, you want location and not locale.
But back location, as I mentioned it has many tricks up its sleeve. We're next going to look at a few of those with the video mapping with the GMap module.
Get unlimited access to all courses for just $25/month.Become a member