PHP code example of vectorial1024 / open-location-code-php
1. Go to this page and download the library: Download vectorial1024/open-location-code-php 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/ */
vectorial1024 / open-location-code-php example snippets
use Vectorial1024\OpenLocationCodePhp\OpenLocationCode;
// This code snippet will center around King's Cross station in London, UK.
// Its main entrance at (51.530812, -0.123767) has the Open Location Code of "9C3XGVJG+8F".
$invalidCode = "11iL!Illi1!!!";
$kingsCrossLatitude = 51.530812;
$kingsCrossLongitude = -0.123767;
$kingsCrossCode = "9C3XGVJG+8F";
// test validity of the code
$validity = OpenLocationCode::isValidCode($invalidCode); // invalid code; returns false
$validity = OpenLocationCode::isValidCode($kingsCrossCode); // valid code; returns true
// create object from code
$invalidObject = OpenLocationCode::createFromCode($invalidCode); // invalid code; throws InvalidArgumentException
$validObject = OpenLocationCode::createFromCode($kingsCrossCode); // returns OpenLocationCode instance
// alternatively, create object from coordinates
$anotherValidObject = OpenLocationCode::createFromCoordinates($kingsCrossLatitude, $kingsCrossLongitude); // returns OpenLocationCode instance
// you may check the code is valid
$validity = $validObject->isValid(); // returns true
$validity = $anotherValidObject->isValid(); // also returns true
// you may also read the code...
assert($kingsCrossCode == $validObject->code); // passes
$code = (string) $validObject; // also, can be explicitly casted to string
assert($kingsCrossCode == $code); // passes
// ...to know that both methods result in the same code
assert($validObject->code == $anotherValidObject->code); // also passes
// but you may not modify the code (create a new instance instead!)
$validObject->code = "something else"; // PHP runtime error: $code is read-only
/* Vectorial1024\OpenLocationCodePhp\OpenLocationCode */
class OpenLocationCode implememts Stringable
{
public readonly string $code;
// The explicit string cast gives $this->code;
public const int CODE_PRECISION_NORMAL = 10;
public static function createFromCode(string $code): self;
public static function createFromCoordinates(float $latitude, float $longitude, int $codeLength = self::CODE_PRECISION_NORMAL): self;
public static function encode(float $latitude, float $longitude, int $codeLength = self::CODE_PRECISION_NORMAL): string;
public function decode(): Vectorial1024\OpenLocationCodePhp\CodeArea;
public function shorten(float $referenceLatitude, float $referenceLongitude): self;
public function recover(float $referenceLatitude, float $referenceLongitude): self;
public function contains(float $latitude, float $longitude): bool;
// note: if you need to call contains() many times on the same $this, consider decoding $this first, and then call contains() on the resulting CodeArea instance
public static function isValidCode(string $code): bool;
public function isValid(): bool;
public function isFull(): bool;
public function isShort(): bool;
public function isPadded(): bool;
}
/* Vectorial1024\OpenLocationCodePhp\CodeArea */
class CodeArea
{
public readonly float $southLatitude;
public readonly float $westLongitude;
public readonly float $northLatitude;
public readonly float $eastLongitude;
public readonly int $length;
public function getLatitudeHeight(): float;
public function getLongitudeWidth(): float;
public function getCenterLatitude(): float;
public function getCenterLongitude(): float;
public function contains(float $latitude, float $longitude): float;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.