Formatting Polygons

You can format a polygon in different styles.



use Location\Coordinate;
use Location\Polygon;
use Location\Formatter\Polygon\GeoJSON;

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

$formatter = new GeoJSON;

echo $formatter->format($polygon);

The code above produces the output below:


NOTE: Float values processed by json_encode() are affected by the ini-setting serialize_precision. You can change the number of decimal places in the JSON output by changing that ini-option, e. g. with ini_set('serialize_precision', 8).