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

format() public method

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

Usage Example

示例#1
0
 /**
  * @covers Location\Formatter\Coordinate\DecimalDegrees::format
  */
 public function testFormatDefault()
 {
     $polygon = new Polygon();
     $polygon->addPoint(new Coordinate(10, 20));
     $polygon->addPoint(new Coordinate(20, 40));
     $polygon->addPoint(new Coordinate(30, 40));
     $polygon->addPoint(new Coordinate(30, 20));
     $json = '{ "type" : "Polygon" , "coordinates" : [ [ 20, 10 ], [ 40, 20 ], [ 40, 30 ], [ 20, 30] ] }';
     $this->assertJsonStringEqualsJsonString($json, $this->formatter->format($polygon));
 }
GeoJSON