javascript - How to draw multipart geometries in OpenLayers 3? -


ol.interaction.draw has point, linestring, polygon, multipoint, multilinestring, multipolygon , circle type options. couldn't figure out how draw e.g. multipolygon containing several single polygons. here's demo console-logs valid geojson string, however, containing 1 single polygon.

relevant code:

// create draw interaction , add map: drawinteraction = new ol.interaction.draw({ source:vectorsource, type:"multipolygon" }); map.addinteraction(drawinteraction);  // define geojson format: var formatgeojson = new ol.format.geojson();  // set listener on "drawend": drawinteraction.on("drawend", function(e) {     // feature:     var feature = e.feature;      // clone feature:     var featureclone = feature.clone();     // transform cloned feature wgs84:     featureclone.getgeometry().transform('epsg:3857', 'epsg:4326');     // geojson of feature:     var geojson = formatgeojson.writefeature(featureclone);     // log:     console.log(geojson); }); 

i suggest doing like:

var drawinteraction = new ol.interaction.draw({     source: vectorsource,     type: "polygon" }); map.addinteraction(drawinteraction);  var multipolygon = new ol.geom.multipolygon([]); drawinteraction.on('drawend', function(e) {     var         feature = e.feature,         poly = feature.getgeometry()     ;     multipolygon.appendpolygon(poly);     updategeojson(); }); function updategeojson(){     var         formatgeojson = new ol.format.geojson(),         cloned = multipolygon.clone()     ;     cloned.transform('epsg:3857', 'epsg:4326');     var geojson = formatgeojson.writegeometry(cloned);     console.info(geojson); } 

https://jsfiddle.net/jonataswalker/epyuly7x/


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -