phpgeo 提供了地理座標的抽象(包括對不同橢球體的支援),並允許您高精度地計算座標之間的地理距離。
所需的最低 PHP 版本是 8.1。 phpgeo已測試至 PHP 8.3。
新功能只會進入主分支,不會向後移植。
可以為較舊的 PHP 版本安裝較舊版本的phpgeo 。相容性矩陣請參考下表:
PHP版本 | phpgeo版本 | 支援狀態 | 作曲家安裝 |
---|---|---|---|
8.3 | 5.x | 積極的 | composer require mjaschen/phpgeo |
8.2 | 5.x | 積極的 | composer require mjaschen/phpgeo |
8.1 | 5.x | 積極的 | composer require mjaschen/phpgeo |
8.0 | 4.x | composer require mjaschen/phpgeo:^4.0 | |
7.4 | 4.x | composer require mjaschen/phpgeo:^4.0 | |
7.3 | 4.x | composer require mjaschen/phpgeo:^4.0 | |
7.2 | 3.x | 生命終點 | composer require mjaschen/phpgeo:^3.0 |
7.1 | 2.x | 生命終點 | composer require mjaschen/phpgeo:^2.0 |
7.0 | 2.x | 生命終點 | composer require mjaschen/phpgeo:^2.0 |
5.6 | 1.x | 生命終點 | composer require mjaschen/phpgeo:^1.0 |
5.5 | 1.x | 生命終點 | composer require mjaschen/phpgeo:^1.0 |
5.4 | 1.x | 生命終點 | composer require mjaschen/phpgeo:^1.0 |
該文件可在 phpgeo.marcusjaschen.de 上取得。
使用 Composer,只需執行以下命令將其新增至您的composer.json
:
composer require mjaschen/phpgeo
更新專案的composer.json
中的版本約束並執行composer update
或透過執行以下命令需要新版本:
composer require mjaschen/phpgeo:^5.0
phpgeo在 5.x 版本中進行了一些重大更改。請參閱以下列表,以了解已變更的內容以及升級程式碼所需執行的操作。
改變 | 描述 | 行動 |
---|---|---|
從Line 中刪除了setPoint1() 和setPoint2() 方法 | Line 類別現在是不可變的。 | 使用建構函式建立Line 的新實例。 |
刪除了對 PHP 7.3、7.4 和 8.0 的支持 | 不再支援較舊的 PHP 版本。 | 至少升級到 PHP 8.1。 |
從版本 2.0.0 開始,phpgeo 根據 MIT 授權獲得許可。舊版是 GPL 授權的。
資訊:請造訪文件網站以取得包含許多範例的完整且最新的文件!
phpgeo 提供以下功能(請點選連結查看範例):
18° 54′ 41″ -155° 40′ 42″
)此清單不完整,請造訪文件網站以取得完整的文件和範例!
直接使用計算機物件:
<?php
use Location Coordinate ;
use Location Distance Vincenty ;
$ coordinate1 = new Coordinate ( 19.820664 , - 155.468066 ); // Mauna Kea Summit
$ coordinate2 = new Coordinate ( 20.709722 , - 156.253333 ); // Haleakala Summit
$ calculator = new Vincenty ();
echo $ calculator -> getDistance ( $ coordinate1 , $ coordinate2 ); // returns 128130 . 850 (meters ; ≈ 128 kilometers)
或透過注入計算器物件來呼叫座標物件的getDistance()
方法:
<?php
use Location Coordinate ;
use Location Distance Vincenty ;
$ coordinate1 = new Coordinate ( 19.820664 , - 155.468066 ); // Mauna Kea Summit
$ coordinate2 = new Coordinate ( 20.709722 , - 156.253333 ); // Haleakala Summit
echo $ coordinate1 -> getDistance ( $ coordinate2 , new Vincenty ()); // returns 128130 . 850 (meters ; ≈ 128 kilometers)
可以簡化折線以節省儲存空間或頻寬。使用 Ramer–Douglas–Peucker 演算法(又稱 Douglas-Peucker 演算法)進行簡化。
<?php
use Location Coordinate ;
use Location Polyline ;
use Location Distance Vincenty ;
$ polyline = new Polyline ();
$ polyline -> addPoint ( new Coordinate ( 10.0 , 10.0 ));
$ polyline -> addPoint ( new Coordinate ( 20.0 , 20.0 ));
$ polyline -> addPoint ( new Coordinate ( 30.0 , 10.0 ));
$ processor = new Simplify ( $ polyline );
// remove all points which perpendicular distance is less
// than 1500 km from the surrounding points .
$ simplified = $ processor -> simplify ( 1500000 );
// simplified is the polyline without the second point ( which
// perpendicular distance is ~ 1046 km and therefore below
// the simplification threshold)
phpgeo 有一個多邊形實現,可用來確定其中是否包含一個點。多邊形至少由三個點組成。點是Coordinate
類別的實例。
警告:如果多邊形在 180/-180 度子午線兩側都有點,則計算會給出錯誤結果。
<?php
use Location Coordinate ;
use Location Polygon ;
$ geofence = new Polygon ();
$ geofence -> addPoint ( new Coordinate (- 12.085870 ,- 77.016261 ));
$ geofence -> addPoint ( new Coordinate (- 12.086373 ,- 77.033813 ));
$ geofence -> addPoint ( new Coordinate (- 12.102823 ,- 77.030938 ));
$ geofence -> addPoint ( new Coordinate (- 12.098669 ,- 77.006476 ));
$ outsidePoint = new Coordinate (- 12.075452 , - 76.985079 );
$ insidePoint = new Coordinate (- 12.092542 , - 77.021540 );
var_dump ( $ geofence -> contains ( $ outsidePoint )); // returns bool ( false ) the point is outside the polygon
var_dump ( $ geofence -> contains ( $ insidePoint )); // returns bool ( true ) the point is inside the polygon
您可以使用不同的樣式設定座標格式。
<?php
use Location Coordinate ;
use Location Formatter Coordinate DecimalDegrees ;
$ coordinate = new Coordinate ( 19.820664 , - 155.468066 ); // Mauna Kea Summit
echo $ coordinate -> format ( new DecimalDegrees ());
<?php
use Location Coordinate ;
use Location Formatter Coordinate DMS ;
$ coordinate = new Coordinate ( 18.911306 , - 155.678268 ); // South Point , HI , USA
$ formatter = new DMS ();
echo $ coordinate -> format ( $ formatter ); // 18° 54′ 41″ -155° 40′ 42″
$ formatter -> setSeparator ( " , " )
-> useCardinalLetters ( true )
-> setUnits ( DMS :: UNITS_ASCII );
echo $ coordinate -> format ( $ formatter ); // 18° 54' 41" N , 155° 40 ' 42" W
<?php
use Location Coordinate ;
use Location Formatter Coordinate GeoJSON ;
$ coordinate = new Coordinate ( 18.911306 , - 155.678268 ); // South Point , HI , USA
echo $ coordinate -> format ( new GeoJSON ()); // { " type " : " point " , " coordinates " : [ -155.678268, 18.911306 ] }
在提交拉取請求之前,請務必執行所有檢查和測試並確保一切都是綠色的。
composer ci:lint
composer ci:psalm
composer ci:tests
要立即執行所有檢查和測試,只需使用composer ci
。
當然,可以直接使用測試運行器,例如對於 PHPUnit:
./vendor/bin/phpunit
詩篇:
./vendor/bin/psalm
可以使用act在本地運行整個 CI 測試矩陣:
act --rm -P ubuntu-latest=shivammathur/node:latest