A simple library for dealing with (geographical) locations.
use Location\Location;
use Location\Coordinate\Coordinate2d;
use Location\Distance\Haversine;
// Amsterdam, NL.
$amsterdam = new Location(new Coordinate2d(52.3079989, 4.9715451));
// Venlo, NL.
$venlo = new Location(new Coordinate2d(51.3703748, 6.1724031));
// The distance between Venlo and Amsterdam is 132950 meters (132km 950m)
// using the Haversine formula.
$distance = $amsterdam->calculateDistanceFromLocation($venlo, new Haversine());
Please note that UTM, MGRS and USNG coordinate systems are not yet supported.
$ composer require patrickkempff/location
{
"require": {
"patrickkempff/location": "dev-master"
}
}
<?php
require 'vendor/autoload.php';
use Location\Location;
use Location\Coordinate\Coordinate2d;
use Location\Distance\Haversine;
// Amsterdam, NL.
$amsterdam = new Location(new Coordinate2d(52.3079989, 4.9715451));
// Venlo, NL.
$venlo = new Location(new Coordinate2d(51.3703748, 6.1724031));
// The distance between Venlo and Amsterdam is 132950 meters (132km 950m)
// using the Haversine formula.
$distance = $amsterdam->calculateDistanceFromLocation($venlo, new Haversine());
Please note that the recommend way to install Location is via composer. If you really want to install Location manually, you can download Location from the repo and unpack the files into your project.
<?php
require 'path/to/Location.php';
require 'path/to/Coordinate/Coordinate2d.php';
require 'path/to/Distance/Haversine.php';
use Location\Location;
use Location\Coordinate\Coordinate2d;
use Location\Distance\Haversine;
// Amsterdam, NL.
$amsterdam = new Location(new Coordinate2d(52.3079989, 4.9715451));
// Venlo, NL.
$venlo = new Location(new Coordinate2d(51.3703748, 6.1724031));
// The distance between Venlo and Amsterdam is 132950 meters (132km 950m)
// using the Haversine formula.
$distance = $amsterdam->calculateDistanceFromLocation($venlo, new Haversine());