From a484f4470ea16f354f063e5c4f36500a51fbd7e9 Mon Sep 17 00:00:00 2001 From: mbarto Date: Thu, 20 Apr 2017 17:22:25 +0200 Subject: [PATCH] Some fixes to ol3 drawsupport replace command (#1755) * Some fixes to ol3 drawsupport replace command * Fixes indentation --- .../components/map/openlayers/DrawSupport.jsx | 71 +++++++++---------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/web/client/components/map/openlayers/DrawSupport.jsx b/web/client/components/map/openlayers/DrawSupport.jsx index de08672e24..7fec566132 100644 --- a/web/client/components/map/openlayers/DrawSupport.jsx +++ b/web/client/components/map/openlayers/DrawSupport.jsx @@ -61,7 +61,7 @@ const DrawSupport = React.createClass({ render() { return null; }, - addLayer: function(newProps) { + addLayer: function(newProps, addInteraction) { var source; var vector; this.geojson = new ol.format.GeoJSON(); @@ -90,49 +90,46 @@ const DrawSupport = React.createClass({ this.props.map.addLayer(vector); - if (newProps.features && newProps.features > 0) { - for (let i = 0; i < newProps.features.length; i++) { - let feature = newProps.features[i]; - if (!(feature instanceof Object)) { - feature = this.geojson.readFeature(newProps.feature); - } + this.drawSource = source; + this.drawLayer = vector; + if (addInteraction) { + this.addDrawInteraction(newProps); + } - source.addFeature(feature); + this.addFeatures(newProps.features || []); + }, + addFeatures(features) { + features.forEach((geom) => { + let geometry; + + switch (geom.type) { + case "Point": { + geometry = new ol.geom.Point(geom.coordinates); break; + } + case "LineString": { + geometry = new ol.geom.LineString(geom.coordinates); break; + } + case "Polygon": { + geometry = new ol.geom.Polygon(geom.coordinates); break; + } + default: { + geometry = geom.radius && geom.center ? + ol.geom.Polygon.fromCircle(new ol.geom.Circle([geom.center.x, geom.center.y], geom.radius), 100) : new ol.geom.Polygon(geom.coordinates); + } } - } + const feature = new ol.Feature({ + geometry + }); - this.drawSource = source; - this.drawLayer = vector; + this.drawSource.addFeature(feature); + }); }, replaceFeatures: function(newProps) { if (!this.drawLayer) { - this.addLayer(newProps); + this.addLayer(newProps, true); } else { - newProps.features.map((geom) => { - let geometry; - this.drawSource.clear(); - - switch (geom.type) { - case "Point": { - geometry = new ol.geom.Point(geom.coordinates); break; - } - case "LineString": { - geometry = new ol.geom.LineString(geom.coordinates); break; - } - case "Polygon": { - geometry = new ol.geom.Polygon(geom.coordinates); break; - } - default: { - geometry = geom.radius && geom.center ? - ol.geom.Polygon.fromCircle(new ol.geom.Circle([geom.center.x, geom.center.y], geom.radius), 100) : new ol.geom.Polygon(geom.coordinates); - } - } - let feature = new ol.Feature({ - geometry: geometry - }); - - this.drawSource.addFeature(feature); - }); + this.drawSource.clear(); + this.addFeatures(newProps.features || []); } }, addDrawInteraction: function(newProps) {