PHP code example of vpro / geo-calculator

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

    

vpro / geo-calculator example snippets


use GeoCalculator\DistanceCalculator;

$calculator = new DistanceCalculator();

// Calculate distance between New York and London
$distance = $calculator->calculateDistance(
    40.7128, -74.0060,  // New York coordinates
    51.5074, -0.1278    // London coordinates
);

echo "Distance: " . round($distance, 2) . " km";

// Check if location is within 100km radius
$isNearby = $calculator->isWithinRadius(
    40.7128, -74.0060,  // Center point
    40.7614, -73.9776,  // Point to check
    100,                // Radius
    'km'                // Unit ('km' or 'mi')
);

if ($isNearby) {
    echo "Location is within radius";
}

// Convert 100 kilometers to miles
$miles = $calculator->convertDistance(
    100,    // Distance
    'km',   // From unit
    'mi'    // To unit
);

echo "100 km = " . round($miles, 2) . " miles";

public function calculateDistance(
    float $lat1,
    float $lon1,
    float $lat2,
    float $lon2,
    string $unit = 'km'
): float

public function isWithinRadius(
    float $lat1,
    float $lon1,
    float $lat2,
    float $lon2,
    float $radius,
    string $unit = 'km'
): bool

public function convertDistance(
    float $distance,
    string $from,
    string $to
): float