Friday, November 28, 2014

Use Drupal Location module to filter data based on Proximity


In this blog, I will show you how to filter data based on lat/long of content using Drupal Location Module.
This is helpful when you have scenario like you need to filter data within some miles of selected filter.
For e.g. If you have filter of City and you only need to show Hotels which are within 100 Miles from City center.
Pre requirement:
  1. Drupal 7 installed
  2. Ctools Module installed and enabled
  3. Views Module installed and enabled
  4. Views UI Module installed and enabled
  5. Location Module installed and enabled
  6. Location CCK Module installed and enabled

  • Add Location field to the content type in which you want to have location information.
    We will use Article content type which is available by default in Drupal 7.

To add Location field to article content type
  1. Login as Administrator
  2. Goto Structure >>  Content types
  3. Click edit under Operations column for Article
  4. Click Manage Fields on Top Right
  5. Enter Field name, here I have used “Article Location”
  6. Select Location under field type
  7. Click Save

 8. After clicking Save you will have Collection Settings screen. Select the appropriate option that you want in your content type. 
Lets select Coordinate Chooser to Allow and Click Save Settings.

  • Create some articles with below latitude/longitude
1. 40.077021/-74.413147
2. 40.094882/-74.43924
3. 40.102235/-74.436493
4. 40.114839/-74.42688
5. 40.113789/-74.392548
6. 40.104336/-74.380188
7. 36.73884/-73.994293
  • Create a VIEW to display Articles that we created above without any filter and later we will add Proximity filter in the view
  1. Login as Administrator
  2. Goto Structure >> Views
  3. Click Add new view
  4. Give any view name, here I am using List Article
  5. Click save & exit

If you visit http://{domain-name}/ list-article you will see all the articles that we recently created
Now lets explore proximity filter options in view
Edit the VIEW we recently created.
Click Add under Filter Criteria

Filter by Location under Filter Dropdown
Select Location: Distance/Proximity checkbox and click Apply (all displays) button

You will have Configure filter criterion screen.
Here you have different option under Origin.
1. Select Static Latitude/Longitude under Origin and use 40.098346/-74.412843 values for Latitude/Longitude.
2. Enter Distance 100 Miles.
3. Click Apply (all displays).
4. Click Save on right top.
If you revist the view page again http://{domain-name}/ ist-article you will only see 6 Articles intead of 7. You will not see article with lat/long 36.73884/-73.994293 as the distance between 40.098346/-74.412843 and 36.73884/-73.994293 is more than 100 miles.
Other option is “Use PHP code to determine latitude/longitude”. You can write PHP code in filter from admin without touching the code. This is very useful when you want dynamic filter based on user selection.

For more complex PHP code, you can define function in your module file and get lat/long from function defined in module file on which filter will be performed. For e.g. If you have a filter of city and you want all the hotels in that city within 100 miles, you can enter function name for e.g. “get_lat_long” under “PHP code for latitude, longitude” and pass latitude/longitude from function defined in your module file.
Click Apply (all displays)


Below is the reference function which can be in your .module file
function get_lat_long() {
        // $latitude = Get it from GET, POST or DB;
        // $longitude = Get it from GET, POST or DB;
        $array = array(
                'latitude' => (float) $latitude,
                'longitude' => (float) $longitude,
        );
    return $array;
}