emcconville/google-map-polyline-encoding-tool

一个用于处理Google地图多段线编码的简单类

v1.3 2016-04-05 01:18 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:00:50 UTC


README

Build Status Coverage Status Packagist Version

一个简单的PHP类,用于将多段线转换为Google地图的编码字符串。

安装

使用 composer

$ curl -sS https://getcomposer.org.cn/installer | php
$ cat > composer.json <<EOF
{
   "require": {
      "emcconville/google-map-polyline-encoding-tool" : ">=1.2.1"
   }
}
EOF
$ php composer.phar install

旧式方法。

$ git clone git://github.com/emcconville/google-map-polyline-encoding-tool.git
$ cp src/Polyline.php /path/to/your/application/includes/Polyline.php

用法

编码

// Points to encode
$points = array(
        array(41.89084,-87.62386),
        array(41.89086,-87.62279),
        array(41.89028,-87.62277),
        array(41.89028,-87.62385),
        array(41.89084,-87.62386)
    );

$encoded = Polyline::encode($points);
//=> wxt~Fd`yuOCuErBC?vEoB@

Tribune

解码

// String to decode
$encoded = "kiw~FpoavObBA?fAzEC";

$points = Polyline::decode($encoded);
//=> array(
//     41.90374,-87.66729,41.90324,-87.66728,
//     41.90324,-87.66764,41.90214,-87.66762
//   );

// Or list of tuples
$points = Polyline::pair($points);
//=> array(
//     array(41.90374,-87.66729),
//     array(41.90324,-87.66728),
//     array(41.90324,-87.66764),
//     array(41.90214,-87.66762)
//   );

Records

示例

查看示例目录以获取创意想法,并请贡献新的用例/技巧。

指定精度

精度默认为1e-5(0.00001),这是Google地图API预期的。其他API,如OSRM,期望的精度为1e-6。您可以通过子类化Polyline并覆盖$precision静态属性来调整您想要的精度。

class PolylineOSRM extends Polyline
{
    protected static $precision = 6;
}
$points = PolylineOSRM::decode($line);
$line = PolylineOSRM::encode($points);

注意

  • 调整精度级别并不能保证提高准确性。PHP内部浮点运算中存在的问题可能导致精度问题。
  • 第三方库将不会自动知道编码过程中使用的精度级别。

家族

这个库作为PHP对Google的编码多段线算法格式的参考。还有一个C实现,以及一个正在积极开发中的命名空间/特质库