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!
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.