boegdaan/distance

PHP 地理距离库。

dev-master 2015-07-21 21:14 UTC

This package is auto-updated.

Last update: 2024-09-22 21:52:22 UTC


README

Build Status

此库提供

  • 距离计算
  • 距离矩阵计算

注意,此库不提供地理编码功能(对于此类用途,您可以使用此库)。

示例

use Distance\Model\Distance;
use Distance\Model\Coordinate;
use Distance\Exception\PoolError;

// for HTTP providers
$client = new GuzzleHttp\Client();

// fail-safe distance source
$pool = new Distance\ProviderPool([

    // Google distance matrix provider
    new Distance\Provider\GoogleProvider($client),

    // OSRM server with distance_table plugin
    new Distance\Provider\OsrmProvider($client, [
        'baseUrl' => 'https://:2233/',
    ]),

    // graphhopper service
    new Distance\Provider\GraphhopperProvider($client,[
        'key' => 'YOU_API_KEY'
    ]),

    // routexl.com distance matrix
    new Distance\Provider\RoutexlProvider($client, [
        'username' => 'YOU_API_USERNAME',
        'password' => 'YOU_API_PASSWORD',
    ]),

    // optinal (If all previous fails) - math provider
    new Distance\Provider\HaversinaProvider(),
]);

// GPS coordinates
$coords = [
    new Coordinate(48.41, 34.81),
    new Coordinate(48.43, 35.22),
    new Coordinate(48.43, 36.22).
];

// case1: single distance
$distanceObject = $pool->getDistance($coords[0], $coords[1]);
$distanceInMeters = $distanceObject->getDistance(Distance::UNIT_METER);
$distanceInMiles = $distanceObject->getDistance(Distance::UNIT_MILE);


// case2: distance matrix 3x3
$matrix = $pool->getDistanceMatrix($coords);
$distanceInMeters = $matrix->getDistance($coords[0], $coords[1], Distance::UNIT_METER);

安装

使用 composer

$ composer require bogdaan/distance

供应商

当前支持的供应商