PHP code example of philiprehberger / php-geo

1. Go to this page and download the library: Download philiprehberger/php-geo 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/ */

    

philiprehberger / php-geo example snippets


use PhilipRehberger\Geo\Coordinate;
use PhilipRehberger\Geo\Geo;

$nyc = new Coordinate(40.7128, -74.0060);
$la = new Coordinate(33.9425, -118.4081);

$km = Geo::distance($nyc, $la);          // ~3944 km
$miles = Geo::distance($nyc, $la, 'mi'); // ~2451 mi

$km = Geo::distanceVincenty($nyc, $la); // More accurate for long distances

$center = new Coordinate(40.7128, -74.0060);
$box = Geo::boundingBox($center, 10.0); // 10 km radius

$box->contains($center); // true
$box->toArray();          // ['min_lat' => ..., 'max_lat' => ..., 'min_lng' => ..., 'max_lng' => ...]

$polygon = [
    new Coordinate(40.800, -73.970),
    new Coordinate(40.800, -73.950),
    new Coordinate(40.760, -73.950),
    new Coordinate(40.760, -73.970),
];

$point = new Coordinate(40.780, -73.960);
Geo::contains($polygon, $point); // true

$bearing = Geo::bearing($nyc, $la); // Initial bearing in degrees (0-360)

$mid = Geo::midpoint($nyc, $la); // Geographic midpoint

// Travel 100 km north from the equator
$start = new Coordinate(0.0, 0.0);
$dest = Geo::destination($start, 0.0, 100.0); // bearing=0 (north), 100 km

$bearing = Geo::bearing($nyc, $la);
$direction = Geo::compassDirection($bearing); // e.g. 'SW'

Geo::compassDirection(0.0);   // 'N'
Geo::compassDirection(90.0);  // 'E'
Geo::compassDirection(225.0); // 'SW'

$coord = new Coordinate(40.7128, -74.0060);
$elevated = $coord->withElevation(100.5); // New Coordinate with elevation
$elevated->getElevation(); // 100.5
$coord->getElevation();    // null (original unchanged)

$polygon = [
    new Coordinate(0.0, 0.0),
    new Coordinate(0.0, 1.0),
    new Coordinate(1.0, 1.0),
    new Coordinate(1.0, 0.0),
];

$area = Geo::area($polygon); // Area in square meters

$coord = new Coordinate(57.64911, 10.40744);
$hash = Geo::encodeGeohash($coord);          // 'u4pruydqqvj8'
$decoded = Geo::decodeGeohash($hash);        // Coordinate near original
$bounds = Geo::geohashBounds($hash);         // BoundingBox of geohash cell

$coordinates = [
    new Coordinate(38.5, -120.2),
    new Coordinate(40.7, -120.95),
    new Coordinate(43.252, -126.453),
];

$encoded = Geo::encodePolyline($coordinates);  // '_p~iF~ps|U_ulLnnqC_mqNvxq`@'
$decoded = Geo::decodePolyline($encoded);      // Array of Coordinate objects

$route = [
    new Coordinate(40.7128, -74.0060),
    new Coordinate(41.8781, -87.6298),
    new Coordinate(33.9425, -118.4081),
];

$total = Geo::routeDistance($route); // Total distance in km

Geo::distance($a, $b, 'mi');   // miles
Geo::distance($a, $b, 'm');    // meters
Geo::distance($a, $b, 'nmi');  // nautical miles
bash
composer