Location\Formatter\Polyline\GeoJSON::format PHP Method

format() public method

public format ( Polyline $polyline ) : string
$polyline Location\Polyline
return string
    public function format(Polyline $polyline)
    {
        $points = [];
        foreach ($polyline->getPoints() as $point) {
            $points[] = [$point->getLng(), $point->getLat()];
        }
        return json_encode(['type' => 'LineString', 'coordinates' => $points]);
    }

Usage Example

Beispiel #1
0
 /**
  * @covers Location\Formatter\DecimalDegrees::format
  */
 public function testFormatDefault()
 {
     $polyline = new Polyline();
     $polyline->addPoint(new Coordinate(52.5, 13.5));
     $polyline->addPoint(new Coordinate(62.5, 14.5));
     $json = '{ "type" : "LineString" , "coordinates" : [ [ 13.5, 52.5 ], [ 14.5, 62.5 ] ] }';
     $this->assertJsonStringEqualsJsonString($json, $this->formatter->format($polyline));
 }
GeoJSON