phpgeo provides abstractions to geographical coordinates (including support for different ellipsoids) and allows you to calculate geographical distances between coordinates with high precision.
Using Composer, just add the following configuration to your composer.json:
{
"require": {
"mjaschen/phpgeo": "*"
}
}Use the calculator object directly:
<?php
use Location\Coordinate;
use Location\Distance\Vincenty;
$coordinate1 = new Coordinate(19.820664, -155.468066); // Mauna Kea Summit
$coordinate2 = new Coordinate(20.709722, -156.253333); // Haleakala Summit
$calculator = new Vincenty();
echo $calculator->getDistance($coordinate1, $coordinate2); // returns 128130.850 (meters; β128 kilometers)or call the getDistance() method of a Coordinate object by injecting a calculator object:
<?php
use Location\Coordinate;
use Location\Distance\Vincenty;
$coordinate1 = new Coordinate(19.820664, -155.468066); // Mauna Kea Summit
$coordinate2 = new Coordinate(20.709722, -156.253333); // Haleakala Summit
echo $coordinate1->getDistance($coordinate2, new Vincenty()); // returns 128130.850 (meters; β128 kilometers)There exist different methods for calculating the distance between two points. The Haversine formula is much faster the Vincenty's method but less precise:
<?php
use Location\Coordinate;
use Location\Distance\Haversine;
$coordinate1 = new Coordinate(19.820664, -155.468066); // Mauna Kea Summit
$coordinate2 = new Coordinate(20.709722, -156.253333); // Haleakala Summit
echo $coordinate1->getDistance($coordinate2, new Haversine()); // returns 128384.515 (meters; β128 kilometers)You can format a coordinate in different styles.
<?php
use Location\Coordinate;
use Location\Formatter\Coordinate\DecimalDegrees;
$coordinate = new Coordinate(19.820664, -155.468066); // Mauna Kea Summit
echo $coordinate->format(new DecimalDegrees());<?php
use Location\Coordinate;
use Location\Formatter\Coordinate\DMS;
$coordinate = new Coordinate(18.911306, -155.678268); // South Point, HI, USA
$formatter = new DMS();
echo $coordinate->format($formatter); // 18Β° 54β² 41β³ -155Β° 40β² 42β³
$formatter->setSeparator(", ")
->useCardinalLetters(true)
->setUnits(DMS::UNITS_ASCII);
echo $coordinate->format($formatter); // 18Β° 54' 41" N, 155Β° 40' 42" W<?php
use Location\Coordinate;
use Location\Formatter\Coordinate\GeoJSON;
$coordinate = new Coordinate(18.911306, -155.678268); // South Point, HI, USA
echo $coordinate->format(new GeoJSON()); // { "type" : "point" , "coordinates" : [ 18.911306, -155.678268 ] }- Marcus T. Jaschen mjaschen@gmail.com
- Chris Veness - JavaScript implementation of the Vincenty formula for distance calculation
