CakePHP Yahoo GeoPlanet Plugin
A CakePHP plugin for interacting with the Yahoo Geo Planet API. Provides a simple, familiar API for things like searching for places matching a given search term. Can be used to power ‘store finder’ or ‘branch locator’ type functionality.
This is the first of 5 CakePHP plugins I’m releasing that I mentioned in my ReST DataSource plugin for CakePHP post. They all follow the same approach of including CakePHP models that provide simple and familiar methods for accessing their respective APIs.
You can get the plugin from my github account. I’ll not include much information here, the code is fully commented and there’s a readme file that explains installation, usage and the results you get back. If you’ve any questions or find any issues, please post in the comments below, on the github issue tracker or even better, fork, fix and send me a pull request.
I used this plugin to build in a recent work project: a branch locator facebook application for UK network of 103 locations. The application takes the search term from the user, calls YahooGeoPlanetPlace::find(‘places’, array(‘conditions’ => array(‘q’ => ‘search term’))) (actually does it through Controller::paginate()) to find the places matching the search term, if there is only one, the user is redirected to the view with a map showing the 5 closest branches to their selected place, for example, or if there is more than one, the user is first presented with a paginated list of matching places, which they can then click on to confirm the appropriate one, for example.
Once you have a latitude and longitude pair for your point of interest you can use the haversine formula to work our the distance between that point and the points of all the locations in your database, and paginate through that list which is ordered by the closest location first.
The excellent Google Map implementation with high density marker clustering was done by my hero, Mike Rumble.


3 Responses so far
July 23rd, 2011
3:53 pm
[...] search term. Can be used to power ‘store finder’ or ‘branch locator’ type functionality. No Comments Filed under: CakePHP (0 votes, average: 0.00 out of 5, [...]
September 6th, 2011
2:55 pm
Hi Neil,
Thanks for sharing this. I’m trying to implement nearest ‘branches’ functionality for a personal project. I’m also using CakePHP. How did you integrate the Haversine formula within CakePHP?
Look forward to hearing from you soon.
Saeed.
September 7th, 2011
1:10 pm
Hi Saeed,
I created a custom find method called find(‘nearest’) in my ‘Branch’ model and passed in the coords I wanted the search centred around as:
list($latitude, $longitude) = explode(‘,’, $query['coords']);
then included the following ‘field’ in the $query['fields'] key in the ‘before’ state of the custom find method:
$query['fields'][] =
‘( 3959 * acos( cos( radians(‘.$latitude.’) ) * cos( radians( latitude ) ) * cos( radians( longitude ) – radians(‘.$longitude.’) ) + sin( radians(‘.$latitude.’) ) * sin( radians( latitude ) ) ) ) AS distance’;
Leave a comment