gibilogic/element-geocoding

GiBiLogic Elements - 地理编码

dev-master 2017-03-28 13:17 UTC

This package is not auto-updated.

Last update: 2024-09-15 01:22:04 UTC


README

我们经常发现自己反复在不同的项目中编写相同的代码。

我们感到厌烦,因此决定将这些代码片段收集成现成的包。

地理编码元素

此包包含用于处理地理坐标的有用类。

它使用谷歌的地理编码服务;您可以在其 官方文档 中找到更多信息。

安装

使用控制台命令将此包添加到您的应用程序的 composer.json

composer require gibilogic/element-geocoding

或者,如果您正在使用 composer.phar 版本,请使用控制台命令

php composer.phar require gibilogic/element-geocoding

用法

基础

使用 Point 类来管理具有纬度和经度的地理点

$milan = new Point(45.464161, 9.190336);
$rome = new Point(41.893056, 12.482778);

使用 Route 类来管理两点之间的关系

$route = new Route($milan, $rome);
$distance = $route->getDistance();

Route 实例也可以通过使用 compareTo 方法进行比较

$milanRomeRoute = new Route($milan, $rome);
$milanTurinRoute = new Route($milan, $turin);

$comparison = $milanRomeRoute->compareTo($milanTurinRoute);

使用 GoogleGeocodeServicegeocodeAddress 方法从地址获取 Point 实例

$point = $googleGeocodeService->geocodeAddress('via Aldo Moro 48, 25124 Brescia, Italy');

高级

GeocodeableInterface 添加并实现到现有类中

class Address implements GeocodeableInterface
{
    protected $address;
    protected $zipCode;
    protected $city;
    protected $province;

    protected $latitude;
    protected $longitude;

    // ...

    public function getAddressForGeocoding()
    {
        return sprintf('%s, %s %s (%s), Italy',
            $this->address,
            $this->zipCode,
            $this->city,
            $this->province
        );
    }

    public function getCoordinates()
    {
        return new Point($this->latitude, $this->longitude);
    }

    public function setCoordinates(Point $point)
    {
        $this->latitude = $point->getLatitude();
        $this->longitude = $point->getLongitude();
    }
}

然后使用 GoogleGeocodeServicegeocode 方法

$point = $googleGeocodeService->geocode($address);

贡献

您可以通过许多不同方式为这个库的增长做出贡献

  • 创建有关错误或您希望实现的功能的问题
  • 打开有关修复、新功能、测试、文档等的拉取请求
  • 使用这个库并告诉我们 ;)

许可证

查看附带的 许可证 文件。