Wordpress Radius Search


#1

I`ve a question.

i made a business directory with custom post types.
is it possible to let users search after the category or taxonomies and add a radius search?

for example.

i search in category a with a radius search of 10 miles.

hope u understand what i`m talking about :slight_smile:


#2

no solutions ?


#3

It is possible, though it’s rather difficult. You would have to store the locations in post meta fields, and then look for ones that are near the search when you search for a radius. The difficult part is that you have to find a way to calculate distances from each place’s location that is stored in the meta field.


#4

Basically, what you need to do is when a custom post is added/updated it needs to Geocode via Google the Lat and Long from the postcode/zip code etc and store these in separate fields.

Then you add a text input to search, the input for this is Geocoded via Google too to get the lat and long of the location, then parse this through a fairly simple SQL which calculates the distance between the the search lat and long and each of the rows lat and long, then only show ones which are within X miles.

Geocode like so:

$address = 'London, UK';
$lines = explode("
",file_get_contents('http://maps.google.com/maps/geo?q='.urlencode($address).'&output=csv&key='.$google_map_key));
list($x,$x,$lat,$long) = explode(',', $lines[0]);

SQL like so:

$miles = 5; // Show only results within 5 miles
mysql_query("SELECT *, truncate((degrees(acos( sin(radians(`lat`)) * sin( radians('".$lat."')) + cos(radians(`lat`)) * cos( radians('".$lat."')) * cos( radians(`long` - '".$long."') ) ) ) * 69.09),1) as distance FROM `postcodes` WHERE truncate((degrees(acos( sin(radians(`lat`)) * sin( radians('".$lat."')) + cos(radians(`lat`)) * cos( radians('".$lat."')) * cos( radians(`long` - '".$long."') ) ) ) * 69.09),1) <= ".$miles." ORDER BY `distance` ASC");

SQL is just off the top of my head, so hopefully it will work, if not it will point you in the right direction.


#5

what would it cost to develop such a plugin ?
unfournetly i cant write php :slight_smile:


#6

Hello joomlaianer!

If you really prefer custom plugin, then go to my profile and contact me and lets go from there.
I’d be waiting on your mail then.

Regards,
Nicole


#7

Having the Lng/Lat coordinates stored in the postmeta table makes it difficult (if not impossible) to do sorting and advanced distance queries. Ideally, you should have a separate table with appropriate data types for columns (double/float). I’ve done quite a bit with GiS systems in the past so if you are still looking to get this developed, feel free to send me a message via by profile page.

Kevin


#8

I have just found this plugin it works for me on my premiumpress install.

http://www.web-directory-service.com/universalwp/wp-radius-search/