Last updated March 16, 2013. Created by RogerRogers on March 16, 2013.
Log in to edit this page.

The Goal: Five minutes to location based search results

This page will describe how to create a basic Dealer Locator, User Locator, Store Locator, etc with proximity search. The results will be displayed using Views. Your users will be able to search using friendly strings, such as "City, State", "State", "Province", and "Post/Zipcode" and return a result list ordered by proximity/distance.

Background

The result will not show a map, only a simple list of results ordered by proximity.

There is a lot of documentation in the Drupal geolocation community that is focused on creating maps, which can make anyone new to the Drupal geocoding modules confused about where to begin for simple requirements. Fear not, the steps are actually very easy to accomplish, and you will have a 'store locator' running in minutes.

Instructions

Required modules

You must download the latest modules. These instructions have been tested with the 'dev' versions of geocoder, geofield, and addressfield modules. I believe these modules have a dependency on geophp, maybe others, install any dependent modules that you are instructed to when installing. Make sure to download the 'dev' versions!

Steps

Setup your content type

  1. Create a content type for whatever you want to locate (e.g. Dealers, Stores, etc)
  2. In manage fields: Add an addressfield field to the content type (use options as you see fit).
  3. In manage fields: Add a geofield field to the content type. When adding, make sure to select 'Geocode from another field' from the 'widget' dropdown! The geofield option are pretty straightforward, just make sure to select the addressfield field you created earlier for the 'Geocode from field' option.
  4. Now, create a few records with addresses using your new content type.

That was easy! Now, let's setup the view result...

Setup your views query

  1. Create a view, can be any kind. Select the content type you created earlier for the results.
  2. Add a field: '[name of your geofield] - proximity' (there are a number of geocoding related fields, you want the one with '- proximity' appended)
  3. In the field settings: For the 'Source of Origin Point' field, select 'Exposed Geofield Proximity Filter'.
  4. Add any other identifying fields, such as title, address, etc.
  5. Add a new 'Filter Criteria' for your view for the '[name of your geofield] - proximity' field.
  6. Choose 'Expose this filter to visitors, to allow them to change it'
  7. For 'Operator', choose 'Is less than', because you want results that are closer than a certain distance from the value your users enters, e.g. less than 100 kilometers, etc.
  8. For 'Source of Origin Point', choose 'Geocoded Location', which will allow the user enter friendly values such as city, state, zip/postecode, state, etc. (basically, anything you can put into Google location searches).
  9. For Geocoding Service, choose 'Google Geocoder'.
  10. Add a Sort Criteria: '[name of your geofield] - proximity'. Also, remove any other sort filter already existing, since sorting by date first will foil your proximity filter!
  11. Set to sort ascending, since you probably want to show the closest results first.
  12. Set 'Source of Origin Point' to 'Exposed Geofield Proximity Filter'.
  13. Test your new proximity search!

Wow, not bad! I remember coding a dealer locator like this by hand. Ouch. Took a lot longer than five minutes!

Credit: These steps are copied generally from http://drupal.stackexchange.com/questions/54873/geofield-proximity-in-views

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Thanks! - exactly what I needed.

... but with the current versions, remember to set the Geofield to required:
https://drupal.org/node/1904190

(when you get this error:
Notice: Undefined index: default_value in geofield_field_presave() (line 280 of /var/www/test/sites/all/modules/contrib/geofield/geofield.module).
Warning: Invalid argument supplied for foreach() in geofield_field_presave() (line 290 of /var/www/test/sites/all/modules/contrib/geofield/geofield.module).)

This method works for city, town, etc searches. But for search keywords for a country (example: France) it doesn't give the results most of the time. The reason behind this is the geocoding returns a coordinate for a country (the center point of a country). If a node's coordinates are far away from that center, they dont appear in results. How do you solve this problem?

An image to explan this issue: http://i.stack.imgur.com/76dqq.jpg

So good to have such an exact information. It works just fine, the perfect tutorial!
Thank you VERY much.
Florent

----
florent Claude | Open Web Creative

I have done everything that was described above, but the [geofield - proximity] field is not showing on the list generated in Views when I click 'add field'. The geofield field is there, all the addressfield fields are listed, but no proximity.

I have the 'Locations' module installed. Is there a conflict with that?

I am stuck. Any help would be appreciated. I am on Drupal 7.17 and I am pretty current with all the rest of my modules.

Thanks.

There is nothing in the world you can't fix with 3rd normal form...

I read the post more closely and saw that it said "Make sure to download the 'dev' versions!". I backed everything out, re-installed the development version and it all works now.

There is nothing in the world you can't fix with 3rd normal form...

"The result will not show a map, only a simple list of results ordered by proximity."

That understood, can this method be adapted - with the modules listed (i.e. geocoder, geofield, addressfield) - to display a map?

If so, then this statement about Location module on the Location module page (https://drupal.org/project/location) maybe needs contradicting: "At present, it is the only module that provides the views integration necessary for implementing location-based searches and is designed for the purpose of finding points on a map within a vicinity of a user-supplied location."

Yep, you can add a map, see for instance http://drupal.org/project/ip_geoloc.
Yes that paragraph on the Location module project pages is inaccurate. See #2062493: Project page inaccurate.

I am looking for way how create default proximity filter. Source of Origin Point must be geolocation of node. So visitors see every node location around 50 km, but visitors can´t change it. How can I do it?