Tools for interacting with and operating on geographical items
Pull request Compare This branch is even with weejames:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Geotools for CodeIgniter

by James Constable @weejames



A collection of tools for geographic calculations and geocoding for use in CodeIgniter based applications.

Currently the only functions offered are those to calculate the distance between points, specified by a Latitude, Longitude pair.


You can either use to install via the spark installer. Visit to install it.


  1. Extract the archive you get from here.
  2. Put libraries/geotools.php and libraries/geopoint.php into your applications libraries directory.


If you've installed via the spark then add the following to your controller.




if you've installed manually.


To calculate the distance between two points create the two locations by calling the geopoint function.

$startPoint = $this->geotools->geopoint(55.8333, -4.25);
$endPoint = $this->geotools->geopoint(55.9464, -3.1991);

Then call the distanceBetween function to calculate the distance between the two locations.

$this->geotools->distanceBetween($startPoint, $endPoint, $algorithm, $unit);

Two algorithms are available to calculate distance.

  • Pythagorean theory - faster but less accurate over longer distances.
  • Haversine - fairly accurate but has a larger performance hit. This is the default option.

Units supported are 'miles' and 'km'. Miles are returned by default.


To calculate the bearing (in degrees 0-360) between two points

$this->geotools->bearingFrom($startPoint, $endPoint)

You can also pull the rough compass direction using

$this->geotools->compassDirection($startPoint, $endPoint)


To calculate the midpoint of two points

$this->geotools->midpoint($startPoint, $endPoint)

End point.

To calculate an endpoint given a startpoint, bearing (0-360 degrees) and distance.

$this->geotools->endpoint($startPoint, $bearing, $distance, $unit);

The unit refers to 'miles' or 'km' and should match what you're passing in to $distance. It'll default to 'miles'.

Problems? Suggestions?