Creating a Drupal 7 Proximity Search
This Drupal Video Tutorial will show you how to create a proximity search using the Views, Location, Gmap and Chaos Tools modules in Drupal 7. After creating a location content type, we create a view which exposes a proximity search filter.
I also recommend you check out the Improving the UI of Drupal Proximity Searches video tutorial to take your proximity search further.

Comments
Peter, First off, thank you
Peter,
First off, thank you for the excellent tutorial.
The fact that location proximity searches don't work for zip codes within four miles is interesting. Especially since a location within the same zip code as that entered in the filter will may not be returned. For my city, it is actually only within one mile that will not work. I tracked down the reason, but it doesn't seem like an easy fix.
The zipcodes table lists the latitude and longitude from at or near center of that zip code. Therefore, the search is conducted by calculating the proximity between filter's zip code's latitude and longitude, and the latitude and longitude of the zip codes geographical center.
If a zip codes boundaries extend to, 4 miles for example. It is possible for locations to be entered that are in the same zip code as that entered in the filter, yet nearly two miles from the zip code's geographical center. The problem gets worse when dealing with zip codes that span larger geographical areas.
Without the ability to map zip codes geographical boundaries, and performing complex computations to determine whether or not a location is a given proximity from the nearest boundary, this will never work as expected.
The most sensible option would be for a visitor to enter a location, including the street address, into the search filter. Then, the location could be geocoded to obtain a more specific latitude and longitude than what is provided in the zipcodes table. The proximity search would be far more accurate this way.
That being said, I'm with you in proposing a limited choices for the proximity to the end user, with none of them being less than five miles. This still isn't perfect, as I don't know the largest possible radius that any given zip code's boundaries currently extend to.
I hope that makes sense. I wish I could draw a diagram.
In the mean time, I have added an 'or' condition to the query that includes locations having the same postal code as that being searched for.
Jim
Hey Jim, thanks for the
Hey Jim,
thanks for the explanation, definitely makes sense. Makes me wonder if that's why popular store locators / searches on websites have a predefined drop down box for proximities. The ones I see usually offer 5, 10, 15, etc. radiuses. It would make sense to me as the issue you detail doesn't seem specific to Drupal / Location module but geocoding in general.
Also, I was thinking about it, rather than provide users a message that tells / validates nothing less than 5, we could also create a small module that invokes hook_form_alter and just changes the input type of the text box to a select box with the specific options we want users to select. Seems that would be a little more user friendly. The address information makes sense from a technical standpoint but I wonder how it would affect user interaction i.e., would you dissuade users from searching because they had to enter more data? Be interesting to test that...
Anyways, thanks for the comment and support, much appreciated!
Pete
Hi, I followed your
Hi,
I followed your instructions exactly as follows:
davidcasciottis-MacBook-Pro:~ davidcasciotti$ cd Sites/drupal7/sites/all/modulesdavidcasciottis-MacBook-Pro:modules davidcasciotti$ git clone --branch 7.x-4.x http://git.drupal.org/project/location.git
Cloning into location...
remote: Counting objects: 6390, done.
remote: Compressing objects: 100% (2377/2377), done.
remote: Total 6390 (delta 4435), reused 5788 (delta 3945)
Receiving objects: 100% (6390/6390), 5.89 MiB | 57 KiB/s, done.
Resolving deltas: 100% (4435/4435), done.
configured gmap with Google api key
configured gmap location
when I selected bundle tab in location configuration got this error message.
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'drupal_sep6.location_bundles' doesn't exist: SELECT * FROM {location_bundles}; Array ( ) in location_overview_bundles() (line 384 of /Users/davidcasciotti/Sites/drupal7/sites/all/modules/location/location.admin.inc)
I tried many times. Looking for some assistance.
Thanks
really good tut... have you
really good tut...
have you looked at the geolocations features in mongoDB? seems like an interesting solution. ALso, I was having trouble displaying the lat and long using location.
Hi, I followed all your steps
Hi,
I followed all your steps but proximity search is not working properly. I am getting results of all the locations which doesn't satisfy the proximity criteria also. Could you please help me to fix this issue by explaining where the problem will be? thanks in advance.
Regards,
Thangadurai
I have the same issue :(
I have the same issue :(
You need to check to filters
You need to check to filters to "Yes" in Attachment tab!
Inherit contextual filters:Yes
Inherit exposed filters:Yes
Hi all, i recently ran into
Hi all,
i recently ran into this problem on another site and it turns out google maps api was returning a 620 error code -- too many requests. This can happen when you perform too many searches in a single day (approx. 25k / per) or you send too many requests very quickly. The new version of location module for drupal 7 has a setting under the google maps geo coding configuration to provide a time delay between searches.
the reason i mention this is, if you are getting any errors back from google maps api, you will get all results. the way the location module works is, you enter a postal code and hte module will check the zipcode table for the corresponding lat/long of the postal code and add those to the WHERE clause of the query.
If the location module can't find a postal code in your database, it queries your geocoding service (in the configuration). the geocoding service should then return the lat/long of the postal code and it will be added as a WHERE clause to the query.
If the site gets nothing back from google maps, no WHERE query is added and all results are returned. My advice is to turn on query preview in views and make sure you have a where clause. If you don't, make sure you have selected a geocoding service in location module set up and check your site logs to see if there are any errors when you run a search.
Pete
Just wanted to drop a line
Just wanted to drop a line saying thanks for the video. It's currently the only resource I can find online right now showing a simple solution for proximity searches in Drupal 7.
Drush for zip code
Drush for zip code loading
Here's a suggestion for uploading the zip code database.
1. Navigate to the root of your site
2. execute "drush sqlc < /database/zipcodes..mysql"
Thanks for the video. I'm
Thanks for the video. I'm currently trying to follow it as a tutorial but I don't seem to have the Bundles tab when go to the Content Authoring setup for Location. I do have Node Locations and User Locations turned on. Any ideas? thanks!
Hi All, I've followed this
Hi All,
I've followed this video step by step and for some reason the map is not showing. I'm using Location 7.x-3.x-dev because on the drupal site they said "Location 7.x-4.x is an abandoned branch that was originally intended to implement locations as entities".
Any suggestions?
Thanks
Great tutorial! immense help.
Great tutorial! immense help. Thanks a lot for this.
I still have a few quirks that I have to figure out but so far so good.
Do you have a tutorial on how
Do you have a tutorial on how to theme this view? I have an overrided template for this view but I can't seem to get anywhere- I've been stuck on this for a while. Any help would be greatly appreciated! This is due in the next day or two!
Post new comment