Drupal Location Module: Zip Code Proximity Search

Nov 09, 2011
Carlos Martinez

The Drupal Location Module is a community module that allows geographic information to be associated with nodes. A Geographic Information System (GIS) describes any information system that integrates, stores, edits, analyzes, shares, and displays geographic information. In a more generic sense, GIS applications are tools that allow users to create interactive queries (user-created searches), analyze spatial information, edit data, maps, and present the results of all these operations.

Among the mentioned searches we find the Zip Code Proximity Search which will give us a list of places (nodes) that are located within a given distance of the zip code estimate center.

House Cleaning

First of all, you must install the Location and Views modules.
For this particular procedure I am using the following versions:
  • Location: 6.x-3.0
  • Views: 6.x-2.8
Once you install the Location module, you will need to import the zip codes to the database. To achieve this, take a look at the INSTALL.txt file located in the folder where you installed the module.

Alternatively, you can go to the following folder:
{path_to_location_module}\database
and using any MySQL execute window, run the script containing the data for the country you want to do searches against.

The Procedure

1.    Create a new or use an existing Content Template capable of holding location information. Configure the Locative Information to hold one position.

 
2.    Create some content using the Content Type defined before.  For this blog post, I have created the following sample nodes:

Place Latitude Longitude
Oshyn Inc. 34.049886 -118.253961
Dodgertown 34.073919 -118.242803
Griffith Park 34.039503 -118.249197
San Diego Int. Airport 32.733538 -117.189746

Note:
If you have Gmap module installed and enabled, then you will be able to use the Google Maps interface to select the location of a point.

3.    Create a new view. You can name the view anything you like. Choose Node as the View type.
 

4.    Add a filter to the view so you can only see nodes created with the Content Type we defined earlier.

 

5.    Add a Location Distance/Proximity filter.  
  • Chose Proximity Circular as the Operator.
  • Expose the filter.
  • Choose Postal Code as the Form mode.
 

6.    Add the following fields to the view: Node Id (Nid), Node Title.

7.    Add another field to the view, but this time use the Location group and choose Distance/Proximity.
  • On the configuration page for this field, choose the Units you want to use the do the searches (Miles or Kilometers) and set the Origin as Use Distance/Proximity filter.
 

8.    (Optional)Choose a Style for the view. In this case I have used a table style so it’s easier to see the results.

9.    Add a Page Display to the view so you can test the page in your browser.
  • Configure the Path to this page display. You can set it to something like “SearchByZip”.

10.    Save the view.

11.    Open in your browser the page you just created. In my case this is the URL:
http://localhost/drupal/SearchByZip
but yours may differ. You can now input some values for Postal Code and for Distance. You will see the following results.
 

As a side note, if you look at the address bar you will notice that the page uses arguments to do the search, so you can now use those arguments to call the page directly. The arguments are:
  • distance[postal_code]    
  • distance[search_distance]
  • distance[search_units]
Check out Oshyn's white paper on Drupal Multisite Installation options and Oshyn's white paper about Drupal and Social Media.



Download the 
Performance Tuning Drupal ebook.