We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Radius Search

Development and Programming

PDK1024's avatar
PDK1024
3 posts
14 years ago
PDK1024's avatar PDK1024

I’m trying to add a radius search around specified cities. Do you know how to program a site to do a radius search around a specified city?

I’m using the super search module from solspace.

Please point me in the right direction or let me know someone that i can hire.

       
the3mus1can's avatar
the3mus1can
426 posts
14 years ago
the3mus1can's avatar the3mus1can

Have you looked at http://natural-logic.com/software/geofinder-for-expressionengine/?

       
Jamie Rumbelow's avatar
Jamie Rumbelow
546 posts
14 years ago
Jamie Rumbelow's avatar Jamie Rumbelow

It’s funny you’ve posted this as I’ve recently had to tackle this exact problem. I’m storing each bit of data related to a store in a channel with a latitude and longitude. I’m then enabling PHP in templates and performing a pretty nasty trigonometry SQL search to handle the search itself. I stole this from a very good Google article.

<?php
$query = sprintf("SELECT DISTINCT entry_id AS id,
                         field_id_18 AS latitude,
                         field_id_19 AS longitude,
                         field_id_13 AS address,
                         field_id_15 AS telephone,
                         field_id_16 AS website,
                         field_id_17 AS email,
                         ( 3959 * acos( cos( radians('%s') ) * cos( radians( field_id_18 ) ) * cos( radians( field_id_19 ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( field_id_18 ) ) ) ) AS distance 
                         FROM exp_channel_data
                         HAVING distance < '%s' 
                         ORDER BY distance",
mysql_real_escape_string($this->EE->uri->segments[3]),
mysql_real_escape_string($this->EE->uri->segments[4]),
mysql_real_escape_string($this->EE->uri->segments[3]),
mysql_real_escape_string(25));

$result = $this->EE->db->query($query)->result();

As you can see, I’m pulling the searching latitude and longitude from the URI segments - I’m actually encoding this and returning it as JSON for AJAX - but since you’re searching on a specific city, you can get the latitude and longitude for each city and plug them in there. $this->EE->uri->segments[3] maps to latitude and $this->EE->uri->segments[4] maps to longitude. Additionally, the 25 is the radius of the search in miles.

Once you’ve got that $result variable you can do whatever you like with it… loop through it and display HTML or output it as JSON.

Hope that helps!

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.