Mapping With Drupal - Alan Palazzolo [24]
Chapter 4. Displaying Maps
Few Drupal sites are built without the Views module, and Views underlies much of the mapping in Drupal. This chapter explores different ways of displaying spatial data, primarily through the Google and OpenLayers JavaScript mapping APIs, but also using lists, tables, and data feeds.
There are two main choices for displaying maps in Drupal: the OpenLayers JavaScript library and the GMap module’s implementation of the Google Maps API. This chapter covers how to create maps with both modules.
The GMap module is the more straightforward to understand. It contains code to pull data from your Drupal website and display it on a Google Map. Continuing our earlier example, this will be a map of Drupal companies.
The OpenLayers module for Drupal does a similar thing, but it uses a different JavaScript API to display the map, and it allows you to choose from several different base tile sets, including sets from Google Maps, OpenStreetMap, Bing, and more. Continuing our earlier example, this will be a map of Drupal user groups.
Mapping with the OpenLayers Module
OpenLayers is a JavaScript library for displaying maps. It is a free, open source project, which means that you can use the maps you create in any way that you want and that, if you’re so inclined, you can help improve the code. At the most basic level, the OpenLayers module allows maps to be displayed on a Drupal website. The module provides a way to configure the appearance of maps and to choose behaviors for the maps. The display of the data on the map is done by integrating with the Views module. Each of these steps is outlined below.
Basic Configuration of OpenLayers
Download the latest release of the OpenLayers module for Drupal 7 to your site’s contributed modules directory. You will also need the Chaos Tools module and the Views module, as they are required by OpenLayers.
Enable all three modules that come with OpenLayers (Figure 4-1). The OpenLayers UI module provides a user interface for configuring your maps and can be disabled once work is complete on your site. The OpenLayers Views module integrates OpenLayers with the Views module. The Chaos Tools module, Views module, and Views UI module must also be enabled.
Figure 4-1. List of OpenLayers modules
Configure permissions for OpenLayers by following the “Permissions” link on the Modules page, or by going directly to the Permissions page at admin/people/permissions. There is only one permission, “Administer OpenLayers,” which should only be given to trusted administrators. You do not want normal users of your site to change the appearance and behavior of the maps on your site.
Setting Up an OpenLayers Map
In the OpenLayers module, a map is a combination of three things: layers of data (such as map tiles or markers), styles that are applied to some of these layers, and behaviors (interactions that are attached to maps). OpenLayers uses the Views module in two ways: first to create data layers to display on the map, as a map is a set of layers, and second to display the final map as a block or page. We start this exercise by creating a layer using Views, and then will use Views again to create the final map.
NOTE
This is the place where most users get confused with the OpenLayers module, and rightfully so. The module uses Views twice. This means that you will have to go to the Views interface, back to the OpenLayers interface, and back to Views. The goal is to change this in future versions of the OpenLayers module so that the user experience is more