matthiasmullie / geo
地理助手
1.1.2
2023-09-11 19:54 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: >=2.0
- phpunit/phpunit: >=4.8
- squizlabs/php_codesniffer: >=3.0
README
强烈建议您在 https://www.mullie.eu/geographic-searches/ 阅读一些背景信息
用法
查找Kortrijk火车站10公里范围内的任何事物
Geo
类特别适合进行基于距离的简单计算
$geo = new Geo\Geo('km'); // coord of Kortrijk railway station $coord = new Geo\Coordinate(50.824167, 3.263889); // calculate bounding box of 10km around this coordinate $bounds = $geo->bounds($coord, 10); /* * Now pass this on this the database, so it executes a query like: * SELECT * * FROM coordinates * WHERE * lat BETWEEN :swlat AND :nelat * lng BETWEEN :swlng AND :nelng * * :swlat being $bounds->sw->latitude * :swlng being $bounds->sw->longitude * :nelat being $bounds->ne->latitude * :nelng being $bounds->ne->longitude * * Assume we have the database results in a variable called $results */ // now weed out entries that fit in the bounding box, but not exactly in // the radius we want them to be in foreach ($results as $i => $result) { $resultCoord = new Geo\Coordinate($result['lat'], $result['lng']); // actual distance between source coordinate & result from DB $distance = $geo->distance($coord, $resultCoord); // if distance is too large, get rid of the result if ($distance > 10) { unset($results[$i]); } }
聚类坐标
当处理大量地点时,Clusterer
类可能很有用。例如,您想在地图上显示成千上万的地点。Google Maps(可能还有其他)也可以创建簇,但您仍然需要将所有地点传递到前端。而不是输出所有坐标并等待完整列表加载,我们可以在PHP中完成聚类,并向您的客户端提供更轻量级的坐标选择!
$clusterer = new Geo\Clusterer( // your viewport: in this case an approximation of bounding box around Belgium new Geo\Bounds( new Geo\Coordinate(51.474654, 6.344604), new Geo\Coordinate(49.482639, 2.471924) ) ); // create a matrix of about 12 cells (this may differ from 12, depending on // the exact measurements of the bounding box) $clusterer->setNumberOfClusters(12); // start clustering after 2 locations in the same cell $clusterer->setMinClusterLocations(2); // add locations to clusterer $clusterer->addCoordinate(new Geo\Coordinate(50.824167, 3.263889)); // Kortrijk railway station $clusterer->addCoordinate(new Geo\Coordinate(51.035278, 3.709722)); // Gent-Sint-Pieters railway station $clusterer->addCoordinate(new Geo\Coordinate(50.881365, 4.715682)); // Leuven railway station $clusterer->addCoordinate(new Geo\Coordinate(50.860526, 4.361787)); // Brussels North railway station $clusterer->addCoordinate(new Geo\Coordinate(50.836712, 4.337521)); // Brussels South railway station $clusterer->addCoordinate(new Geo\Coordinate(50.845466, 4.357113)); // Brussels Central railway station $clusterer->addCoordinate(new Geo\Coordinate(51.216227, 4.421180)); // Antwerpen Central railway station // now get the results... $clusterer->getClusters(); // returns 1 cluster: all 3 Brussels stations $clusterer->getCoordinates(); // returns 4 non-clustered coordinates
安装
如果您使用 Composer 管理项目依赖,只需在 composer.json 文件中添加对 matthiasmullie/geo
的依赖即可
composer require matthiasmullie/geo
尽管建议使用 Composer,但实际上您可以选择任何方式包含这些文件。
许可协议
Geo 是 MIT 许可。