Howto: Zip/postal code proximity search using views and location (Drupal 6)

Last updated on
30 April 2025

Note: There is currently a bug in Location module that causes searches for large distances to not include results past a certain distance. This distance changes depending on the latitude of the origin point. You can find an estimate of these distances here. Be warned and test within those distances.

This is a tutorial on how to use the Location module to create a view to search by zip code proximity in Drupal 6. Click here for instructions with Drupal 7.
The example is simple so that it can be easily reproduced with these minimal module requirements:

  • Drupal
  • Views
  • Location

Note: It requires at least location version 6.x-3.1 as the proximity views functionality was overhauled for that release.

This view will ultimately accept a user in-putted zip code and distance and return a listing of content within that distance of the zip code, sorted by distance from that zip code.

First, you must install the Location and Views modules and configure Location fields for whatever content type you desire.

Note: This example uses node locations, not CCK location fields (ie. using the Node Locations module instead of the Location CCK module).

The gmap module is also a good addition because it allows the view to be displayed as location markers on a map instead of as a text-based list.
This tutorial will not cover use of the gmap module.

Next, import the zip codes from the country of your choice as per the location module installation guide (you can also find information in this in the INSTALL.TXT file found in the location module).
Note: Proximity searches by post code will not work without this step because those database files contain the latitude/longitude values of the post codes.

Steps to create the view (this assumes basic views knowledge):

  1. Create new view of type Node (you can also use other types if they better suit your requirements).
  2. Select Fields for the Row style setting (you can use any row style that suits your requirements).
  3. Add any Fields you want for you view (this example uses the Node: Title and Location: Address fields).
  4. Add any non-proximity Filters that you want for your view (this example uses Node: Type = 'page' and Node: Published = Yes.
  5. Add the Location: Distance / Proximity filter, expose it and give it the following settings (see attached files for a screenshot of these settings):
    • Operator: Choose what type of proximity search you want. This example uses Proximity (Circular)
    • Origin: Postal Code (assume default country). You could also use Postal Code / Country if you want the user to also select the country.
    • Postal code: You can leave this blank or provide a post code, which will be used as the default value. In this example it is left blank.
    • Distance: You can leave this blank or provide a post code, which will be used as the default value. Also select whether you want to use miles or kilometres for the distance. This example uses 100 miles.
    • Unlock operator: Unchecked in this example but can be checked if you require.
    • Optional: Checked in this example but can be check if you require.
    • Remember: Unchecked in this example but can be checked if you require.
    • Filter identifier: distance in this example but can be anything you require.
    • Label: Proximity in this example but can be anything you require.
    • Allow choice of user location: Unchecked. This option has no affect on the post code related Origins.
  6. Add the Location: Distance / Proximity sort and give it the following settings:
    • Origin: Use Distance / Proximity filter (this links the sort criteria to the filter that collected the user's entered zip code).
    • Sort order: Ascending

(see attached files for a screenshot of the whole view's settings.)

I have also attached a text file containing an export of my example view to use as a starting point if you desire.

Help improve this page

Page status: Not set

You can: