PHP code example of frostybee / geobee
1. Go to this page and download the library: Download frostybee/geobee 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/ */
frostybee / geobee example snippets
use Frostybee\Geobee\Calculator;
/*
* Calculate the distance from downtown Montreal to Laval.
* From: Ville-Marie
* postal code area: H3A
* Latitude/Longitude: 45.4987, -73.5703
* To: Laval
* postal code area: H7T
* Latitude/Longitude: 45.5569, -73.7480
*/
$calculator = new Calculator();
$distance = $calculator->calculate(
45.4987, // From latitude
-73.5703, // From longitude
45.5569, // To latitude
-73.7480 // To longitude
)->getDistance(); // Returns distance in meters (e.g., 15297.83)
use Frostybee\Geobee\Calculator;
$calculator = new Calculator();
$distance = $calculator->calculate(
45.4987,
-73.5703,
45.5569,
-73.7480
)->to('km'); // Returns distance in kilometers (e.g., 15.298)
use Frostybee\Geobee\Calculator;
$calculator = new Calculator();
// Syntax: to(unit, decimals, round)
$distance = $calculator->to('mi', 3, false); // 3 decimals, use floor rounding
$distance = $calculator->to('km', 2); // 2 decimals, default round (true)
use Frostybee\Geobee\Calculator;
$calculator = new Calculator();
$distance = $calculator->calculate(45.4987, -73.5703, 45.5569, -73.7480);
// Convert to specific units using toMany()
$results = $calculator->toMany(['km', 'mi'], 3, true);
/*
Returns:
array(2) {
["km"] => float(15.298)
["mi"] => float(9.506)
}
*/
// Convert to all supported units using toAll()
$distance_in_all_units = $calculator->toAll(2, true);
/*
Returns:
array(6) {
["m"] => float(15297.83)
["km"] => float(15.30)
["ft"] => float(50189.72)
["yd"] => float(16729.91)
["mi"] => float(9.51)
["nm"] => float(8.26)
}
*/
use Frostybee\Geobee\Calculator;
$calculator = new Calculator();
// Validate coordinates before calculation
if ($calculator->isCoordinate(45.4987, -73.5703)) {
$distance = $calculator->calculate(45.4987, -73.5703, 45.5569, -73.7480)
->to('km');
echo "Distance: $distance km";
} else {
echo "Invalid coordinates provided.";
}
$calculator->isCoordinate(45.4987, -73.5703); // Valid: true
$calculator->isCoordinate(91, 50); // Invalid: latitude > 90
$calculator->isCoordinate(45, 181); // Invalid: longitude > 180
$calculator->isCoordinate("45.5", "-73.7"); // Valid: strings are accepted
$calculator->isCoordinate([45], [73]); // Invalid: arrays not allowed
use Frostybee\Geobee\Calculator;
use Frostybee\Geobee\InvalidCoordinateFormatException;
use Frostybee\Geobee\InvalidUnitException;
try {
$calculator = new Calculator();
$distance = $calculator->calculate(45.4987, -73.5703, 45.5569, -73.7480)
->to('km', 2);
echo "Distance: $distance km";
} catch (InvalidCoordinateFormatException $e) {
echo "Invalid coordinates: " . $e->getMessage();
} catch (InvalidUnitException $e) {
echo "Invalid unit: " . $e->getMessage();
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
use Frostybee\Geobee\Calculator;
$calculator = new Calculator();
// Chain multiple operations
$km = $calculator->calculate(45.4987, -73.5703, 45.5569, -73.7480)->to('km', 2);
$mi = $calculator->to('mi', 2); // Reuse the same calculation
$nm = $calculator->to('nm', 2);
use Frostybee\Geobee\Calculator;
$calculator = new Calculator();
$distance = $calculator->calculate(45.5, -73.7, 45.5, -73.7)->getDistance();
// Returns: 0
$km = $calculator->to('km');
// Returns: 0
$all = $calculator->toAll();
// Returns: [] (empty array)
use Frostybee\Geobee\Calculator;
$calculator = new Calculator();
$calculator->calculate(45.4987, -73.5703, 45.5569, -73.7480);
// No rounding (returns full precision)
$distance = $calculator->to('km'); // e.g., 15.29783...
// With decimals (1-9 allowed)
$distance = $calculator->to('km', 2); // e.g., 15.30
$distance = $calculator->to('km', 5); // e.g., 15.29783
// Rounding behavior
$distance = $calculator->to('km', 2, true); // Standard rounding (PHP_ROUND_HALF_UP)
$distance = $calculator->to('km', 2, false); // Floor rounding (PHP_ROUND_HALF_DOWN)