EncodedPolyline: Added geometryType attribute

This makes it possible to read polygons or multipoints too. Since the
encoded format is just a list of points the reader needs to be told what
Feature to create from the encoded list.

The example code is edited to reflect that API extension.
This commit is contained in:
Tobias Bieniek
2012-12-27 21:36:30 +01:00
parent 88a3091a90
commit aedafc0336
3 changed files with 61 additions and 8 deletions

View File

@@ -18,6 +18,13 @@
*/
OpenLayers.Format.EncodedPolyline = OpenLayers.Class(OpenLayers.Format, {
/**
* APIProperty: geometryType
* {String} Geometry type to output. One of: linestring (default),
* linearring, multipoint or polygon
*/
geometryType: "linestring",
/**
* Constructor: OpenLayers.Format.EncodedPolyline
* Create a new parser for encoded polylines
@@ -44,6 +51,16 @@ OpenLayers.Format.EncodedPolyline = OpenLayers.Class(OpenLayers.Format, {
* {<OpenLayers.Feature.Vector>} A vector feature with a linestring.
*/
read: function(encoded) {
var geomType;
if (this.geometryType == "linestring")
geomType = OpenLayers.Geometry.LineString;
else if (this.geometryType == "linearring")
geomType = OpenLayers.Geometry.LinearRing;
else if (this.geometryType == "multipoint")
geomType = OpenLayers.Geometry.MultiPoint;
else if (this.geometryType != "polygon")
return null;
var points = new Array();
var lat = 0;
@@ -76,8 +93,15 @@ OpenLayers.Format.EncodedPolyline = OpenLayers.Class(OpenLayers.Format, {
points.push(new OpenLayers.Geometry.Point(lon * 1e-5, lat * 1e-5));
}
if (this.geometryType == "polygon")
return new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Polygon([
new OpenLayers.Geometry.LinearRing(points)
])
);
return new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LineString(points)
new geomType(points)
);
},