Reputation: 26598
I have an openlayers 3 map where I can draw Polygon.
I would have returned the WKT String that rapresent the drawed polygon.
How Can I do it?
JSFiddle Code http://jsfiddle.net/michelejs/3zawt33b/7/
Here my map:
map = new ol.Map({
target: 'map',
layers: [raster,vector],
view: new ol.View({
center: ol.proj.fromLonLat([11.249367, 43.774298]),
zoom: 15
})
});
Here the intaractions that help me to draw the polygon:
function addInteraction() {
var ct = 0;
draw = new ol.interaction.Draw({
source: source,
type: 'Polygon',
geometryFunction: function (c, g) {
if (goog.isDef(g)) {
g.setCoordinates(c);
} else {
g = new ol.geom.Polygon(c);
}
if (c[0].length > ct) {
console.log('click coord : ' + c[0][c[0].length - 1]);
var coord = c[0][c[0].length - 1];
$('div#coordinate').html( $('div#coordinate').html() + "<p>" + ( Number(coord[0]).toFixed(2) ) + " - " + ( Number(coord[1]).toFixed(2) ) + "</p>" );
coordinates.push(coord);
ct = c[0].length;
} else {
console.log('move coord : ' + c[0][c[0].length - 1]);
}
return g;
}
});
draw.on('drawend', function(e) {
isin = e;
checkIfIn();
lastFeature = e.feature;
//write WKT Polygon Code in div#getAsWK
})
draw.on('drawstart', function (e) {
source.clear();
});
map.addInteraction(draw);
}
map.addInteraction(draw);
Upvotes: 2
Views: 5346
Reputation: 119
ol3 contains the ol.format.WKT class for this purpose. Use writeGeometry() method like this:
var format = new ol.format.WKT(),
wkt = format.writeGeometry(yourFeature.getGeometry());
See API-docs: http://openlayers.org/en/v3.0.0/apidoc/ol.format.WKT.html#writeGeometry
Fiddle: http://jsfiddle.net/igor23/3zawt33b/9/
Upvotes: 5