CodeIgniter Model implementing the haversine formula, giving great-circle distances between two points on a sphere from their longitudes and latitudes.
Just drop this model into your application/models
folder (download or git)
$this->load->model('haversine');
$this->haversine->table_name = 'locations';
$locations = $this->haversine->closest(36, -80); //lat, lng
$locations = $this->haversine->closest(36, -80, 25); //25 Miles
$locations = $this->haversine->closest(36, -80, 25, 10); //10 Locations
$locations = $this->haversine->closest(36, -80, 25, 10, 'miles'); //miles or kilometers
$locations = $this->haversine->closest(36, -80, 25, 10, 'miles', array('id, name')); //false for * or single dim array
- More units of measurement (feet, meters)
- Support joins (ie join company meta to a store locations table)
- Change direction of result
- Put
units
param right after longitude - Refractor into a more general GIS model?
- Only tested on MySQL