PHP code example of emcconville / google-map-polyline-encoding-tool

1. Go to this page and download the library: Download emcconville/google-map-polyline-encoding-tool library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

emcconville / google-map-polyline-encoding-tool example snippets


// 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@

// 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)
//   );

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