PHP code example of anthonymartin / geo-location

1. Go to this page and download the library: Download anthonymartin/geo-location library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

anthonymartin / geo-location example snippets



AnthonyMartin\GeoLocation\GeoPoint;

$start = new GeoPoint(52.205, 0.119);
$end = new GeoPoint(48.857, 2.351);
$kilometers = $start->distanceTo($end, 'km'); // about 404.3 km
$moved = $start->withLatitude(53); // $start is unchanged


AnthonyMartin\GeoLocation\GeoPoint;

$start = new GeoPoint(52.205, 0.119);
$destination = $start->destination(10, 90, 'km'); // 10 km east
$bearing = $start->bearingTo($destination);
$midpoint = $start->midpointTo($destination);


AnthonyMartin\GeoLocation\GeoPoint;

$center = new GeoPoint(40.7, -74.0);
$box = $center->boundingBox(5, 'km');
$inside = $box->contains(new GeoPoint(40.71, -74.01));
$southWest = $box->getSouthWest();
$corners = $box->getPerimeter(); // SW, SE, NE, NW


AnthonyMartin\GeoLocation\GeoPoint;
use AnthonyMartin\GeoLocation\Interfaces\Region;

$circle = (new GeoPoint(40.7, -74.0))->circle(5, 'km');
$check = static fn(Region $region): bool => $region->contains(new GeoPoint(40.71, -74.01));
$inside = $check($circle);
$envelope = $circle->boundingBox();


AnthonyMartin\GeoLocation\GeoPoint;
use AnthonyMartin\GeoLocation\Polygon;

$polygon = Polygon::fromArray([[0, 0], [0, 1], [1, 1], [1, 0]]);
$inside = $polygon->contains(new GeoPoint(0.5, 0.5));
$areaKm2 = $polygon->area('km');
$perimeterKm = $polygon->perimeter('km');
$reversed = $polygon->reversed(); // $polygon is unchanged