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
- Create a content type for whatever you want to locate (e.g. Dealers, Stores, etc)
- In manage fields: Add an addressfield field to the content type (use options as you see fit).
- 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.
- 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
- Create a view, can be any kind. Select the content type you created earlier for the results.
- Add a field: '[name of your geofield] - proximity' (there are a number of geocoding related fields, you want the one with '- proximity' appended)
- In the field settings: For the 'Source of Origin Point' field, select 'Exposed Geofield Proximity Filter'.
- Add any other identifying fields, such as title, address, etc.
- Add a new 'Filter Criteria' for your view for the '[name of your geofield] - proximity' field.
- Choose 'Expose this filter to visitors, to allow them to change it'
- 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.
- 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).
- For Geocoding Service, choose 'Google Geocoder'.
- 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!
- Set to sort ascending, since you probably want to show the closest results first.
- Set 'Source of Origin Point' to 'Exposed Geofield Proximity Filter'.
- 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
Thanks! - exactly what I
Thanks! - exactly what I needed.
Nice
... 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,
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
Proximity by Country
Did you ever find a solution to this, Sinan?
Just perfect
So good to have such an exact information. It works just fine, the perfect tutorial!
Thank you VERY much.
Florent
[geofield - proximity] not showing in Views field list
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.
Figured it out - "use development version"
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.
Location based searching and maps
"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."
Yes you can add a map to show markers within a proximity
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.
Default proximity filter
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?
What modules would I require
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.
Using with rendered entity or other non-field based displays?
Is it possible to implement a proximity search with a display that doesn't use fields?
How?
"Location: Distance / Proximity" field displays zero
* resolved *
How?
How did you resolve this? I'm having the same problem.
=-=
Tried this?
This module https://www
This module https://www.drupal.org/project/google_store_locator does a good store locator with maps.
www.raincreativelab.com
Super easy
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.
No longer need dev versions
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.
Dealing with no filter location specified
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?
Lotta Web Design
Answering my own question.
The way I used was to create a PHP filter like this...
return is_null($row->field_geofield_distance);
Lotta Web Design
Limit the search proximity area?
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?
Question - will this work for an already existing content type?
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
Yes, you can
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.
I had some weird things
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:
Ideas on how to fix?
De-install module
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.
Reduce search radius
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?
Thank you.! its help me a lot
Thank you.! its help me a lot.
Views attachment: Inherit exposed filters
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.
Add distance unit to search results
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.
Hey guys, ignore my last
Hey guys, ignore my last comment. Discovered the 'suffix' field in the geo-proximity field that allows me to add 'miles' after the number
Google Map pin point
Hi,
Is it possible to add google map in this front end view to show pin point of all locations?
thanks,
Satheesh
Working?
Did this work for you?
Incorporating map points
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
Thanks, It worked for me.
Thanks,
It worked for me.
Thanks & Regards,
Ajay Singh
If you were going to do this
This is two years old now. If you were going to do this today - would you do it the same way?
Leaflet
You may want to take a look at Leaflet.
I have the most recent
I had a problem with getting no results. Note to anyone adding this retroactively to a view
"Geocode from another field" not available.
When I tried the "Geocode from another field" widget was not showing up. Anybody else having this problem?
=-=
@Wilson: make sure all the required (sub)modules are enabled and that you're using an address field in your content type.
I have exactly the same
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...
I double checked my modules
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.
Hey,
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?
Hello,
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.
Développeur Drupal Freelance — Design Web & Intégration
Indeed according to this
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.
Développeur Drupal Freelance — Design Web & Intégration