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

Comments

johnhorning’s picture

Thanks! - exactly what I needed.

teek’s picture

... 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).)

Sinan Erdem’s picture

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

anboscaban’s picture

Did you ever find a solution to this, Sinan?

ezoulou’s picture

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

Ilikejava’s picture

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.

Ilikejava’s picture

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.

mttjn’s picture

"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."

RdeBoer’s picture

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.

siva01’s picture

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?

end user’s picture

What modules would I require to only have a distance filter and the view would filter according to the current users geoip location even if its an anonymous user.

AaronBauman’s picture

Is it possible to implement a proximity search with a display that doesn't use fields?
How?

pauln60’s picture

* resolved *

Wilson_of_Prussia’s picture

How did you resolve this? I'm having the same problem.

knalstaaf’s picture

Tried this?

Mackee’s picture

This module https://www.drupal.org/project/google_store_locator does a good store locator with maps.

Yazzbe’s picture

Thanks a million !

I spend hours testing different set-ups, but this is absolutely the fastest way to configure proximity search in Drupal 7.

Very well explained.

AdamPS’s picture

Since the release of Geofield 7.x-2.3 on 2014-Sep-21, it seems to work with the full (non-dev) release. I've only done a quick test so far, so I would appreciate another user to confirm.

jaylotta’s picture

When I first display a page using the dealer locator view, there is no value for the exposed location. So I am getting all of the possible values with 0 distance. Is there a way to not show results if no location is specified?

jaylotta’s picture

The way I used was to create a PHP filter like this...

return is_null($row->field_geofield_distance);

arem4ou’s picture

In my implementation of this, we are hardcoding the proximity to 50 miles and searching for a city name is returning no results. It works for some, but not for others.

We're pretty sure it's defaulting to a city of the same name in another state. If possible, I'd prefer not to require my user to type a comma with the state abbreviation after the city name.

Is there any way to limit the area in which it tries to find results from the Google Maps API? Or something along those lines?

Steve -cc’s picture

Can I add the geofield to an already existing content type that uses an addressfield field - Postal address.
ie: start at step 3 instead of step one. Will that work?
Will a new view using this capability see content that was created before adding the new geofield? or will it only see content that was created / added after the geofield field was part of the content type?
One commenter said that the geofield must be set to required - is that still true?

Steve

knalstaaf’s picture

Yes, you can.

I don't think it has to be set to required explicitely, but it needs to be filled out in order to work ofcourse. Check the "Enable geocoding of location data" option in the settings of your Geofield, then it will provide a button to convert a WKT input (such as "New York") to geocode.

Steve -cc’s picture

I had some weird things happen when I did this - started from Step 3 on an existing content type.
Most but not all of the weird things were fixable. The two that I have not been able to fix - even after disabling all of the geo modules and deleting the geofield from the content type are:

  1. The Postal address is now a required field. No matter how many times I make sure the box is unchecked - check it and uncheck it save and re-save. When I go to add content on this content type - The address stays required.
  2. The Postal address - Help text shows twice when I go to add content. No matter how many times I delete it save find it gone then re-write it and save it. When there is help text it get printed twice when I go to add content.

Ideas on how to fix?

knalstaaf’s picture

Would it be a big loss if you'd disable and deinstall the module, to clear all possible remains in the database? If not, I'd consider it.

I'm not sure, but this could be an issue with the address field module as well. So if it's not fixed by deinstalling the Geofield module, maybe it will when doing the same for the address field module.

showFocus’s picture

I need to reduce the minimum search area from 1 to 0.25. Are there any recommendations how to change the module to accomplish this?

kkalaskar’s picture

Thank you.! its help me a lot.

knalstaaf’s picture

In case you're using this as a Views attachment, don't forget to check "Inherit exposed filters" in the attachment settings, or it will only display the distance from the default value, or "0" in case there's no given default value.

oalkirwi’s picture

Great instructions and love the result for our clients site. Can anyone tell me how I can have the distance unit included in the Views results? At the moment I'm displaying the Views output as a table but I don't want the columns to have labels so my results show 5.4, 8.8 etc. but no mention of miles or kilometers. I know this may be seen as pedantic but I'm expecting the client to ask if miles/kilometers can be included in the results.

oalkirwi’s picture

Hey guys, ignore my last comment. Discovered the 'suffix' field in the geo-proximity field that allows me to add 'miles' after the number

satrece’s picture

Hi,

Is it possible to add google map in this front end view to show pin point of all locations?

thanks,
Satheesh

anthony0perez’s picture

Did this work for you?

Anonymous’s picture

Yes, it is - by integrating more modules ....

I used modules leaflet (very nice interactive JavaScript mapping) and ip_geoloc to make it work.
They depend on a bunch of others. In my case, addressfield, geofield, geocoder, and geophp.

There is a good recording of a presentation given by someone in Australia on mapping (sorry, didn't
bookmark it), and this link, which has details on setting up the proximity filter.
https://www.drupal.org/node/1944648

ajay547’s picture

Thanks,
It worked for me.

Thanks & Regards,

Ajay Singh

Steve -cc’s picture

This is two years old now. If you were going to do this today - would you do it the same way?

knalstaaf’s picture

You may want to take a look at Leaflet.

rkent_87’s picture

I had a problem with getting no results. Note to anyone adding this retroactively to a view

  1. Make sure you have a field that's geocoded from the address field. Don't worry - it doesn't show on the node/edit form
  2. If you add this new geocoded field to content that already has address data you will need to re-edit and save each node in order to populate the geofield. VBO has an "Execute PHP code" action. Run it, and input "node_save($entity);" (without the quotes) in the textbox. That will do the trick.
Wilson_of_Prussia’s picture

When I tried the "Geocode from another field" widget was not showing up. Anybody else having this problem?

knalstaaf’s picture

@Wilson: make sure all the required (sub)modules are enabled and that you're using an address field in your content type.

row015’s picture

I have exactly the same problem, or so I think.
When I add the geofield "geocode from another field", I can save the default store location and then, after saving, I get a 500 server error page...

Wilson_of_Prussia’s picture

I double checked my modules and, sure enough, I didn't have every module enabled. I don't feel bad though, nobody ever said I had common sense.

A R J’s picture

Hey,
I am using Google store locator module, and everything is working fine with views. I am getting results too in left section which is search section. But on map, i am getting all the location search result. How can i get the search result on map too?

Nicolas Bouteille’s picture

Hello,
I need to do exactly that but on Drupal 8.
Your instructions don't seem to work anymore for D8.
Step 3, on the field settings: For the 'Source of Origin Point' field, select 'Exposed Geofield Proximity Filter' is not available. Only available option is Geofield manual origin :(
Step 8 : For 'Source of Origin Point', choose 'Geocoded Location', is not available either. Only available option is Geofield manual origin here as well.

Nicolas Bouteille’s picture

Indeed according to this issue #2654360: Re-implement the Views proximity filter/field for Drupal 8, views proximity field/filter does not yet work for Drupal 8 and some work is in progress.