vectorial1024/open-location-code-php

在PHP中实现的Open Location Code(又称“Plus Code”)

dev-master 2024-10-03 08:45 UTC

This package is auto-updated.

Last update: 2024-10-03 08:45:54 UTC


README

Packagist License Packagist Version Packagist Downloads PHP Dependency Version GitHub Actions Workflow Status

这是一个现代PHP版本的google/open-location-code仓库。请访问https://github.com/google/open-location-code以获取关于Open Location Code("OLC",又称"Plus Code")的讨论和最新信息。本仓库只关注PHP实现。

但仍然,以下是从该仓库中摘录的一段关于Open Location Code(OLC)的简要介绍

Open Location Code是一种技术,它以比经纬度更易用的形式对位置进行编码。生成的代码称为plus codes,因为它们的一个区别性属性是包含"+"字符。

一个外部演示可用于展示Open Location Code的功能。

实现说明

此PHP实现是从google/open-location-codeJava实现改编而来。

此实现尽可能使用最新的PHP功能。

许可遵循google/open-location-code

安装

通过Composer

composer require vectorial1024/open-location-code-php

特别说明:为确保32位PHP兼容性,此库使用float变量而不是int变量来计算Open Location Codes。这可能会在某些未预见的边缘情况下导致不精确,但一般来说,应该没有问题。

示例代码

检查和创建OLC代码

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

其他参考资料

可用类/方法的快速参考;请参阅PHPDoc以获取详细信息。

OpenLocationCode:

/* 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;

    public static function isValidCode(string $code): bool;
    public function isValid(): bool;
    public function isFull(): bool;
    public function isShort(): bool;
    public function isPadded(): bool;
}

CodeArea:

/* 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;
}

测试

通过PHPUnit;首先确保PHPUnit已正确设置

composer update

然后

composer run-script test