【发布时间】:2015-09-21 21:40:23
【问题描述】:
ol.interaction.Draw 具有 Point、LineString、Polygon、MultiPoint、MultiLineString、MultiPolygon 和 Circle 作为类型选项。我无法弄清楚的是如何实际绘制例如包含多个单个多边形的 MultiPolygon。 Here's a demo 控制台记录了一个有效的 GeoJSON 字符串,但是,它只包含一个多边形。
相关代码:
// create draw interaction and add it to the 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) {
// get feature:
var feature = e.feature;
// clone feature:
var featureClone = feature.clone();
// transform cloned feature to WGS84:
featureClone.getGeometry().transform('EPSG:3857', 'EPSG:4326');
// get GeoJSON of feature:
var geojson = formatGeoJSON.writeFeature(featureClone);
// log:
console.log(geojson);
});
【问题讨论】:
-
您正在覆盖
var geojson而不是推送新的多边形。 -
好的,我明白了!我试过
features.push(featureClone)和var geojson = formatGeoJSON.writeFeatures(features);This,悬停,创建一个FeatureCollection。 -
所以我需要用
ol.geom.MultiPolygon显式创建一个MultiPolygon(通过提供它的坐标),然后用appendPolygon附加每个额外的多边形?