繁体   English   中英

Openlayers 3 KML导出writeFeatures()不起作用

[英]Openlayers 3 KML export writeFeatures() not working

我正在使用OpenLayers版本:v3.13.0,并且尝试导出图层中的所有要素。 我的代码如下

 var projection = ol.proj.get('EPSG:3857'); var format = new ol.format.KML({ 'maxDepth': 10, 'extractStyles': true, 'internalProjection': projection, 'externalProjection': projection }); var newfeatures = []; var vectorSource = layer.getSource(); vectorSource.forEachFeature(function(feature) { var clone = feature.clone(); clone.setId(feature.getId()); // clone does not set the id clone.getGeometry().transform(projection, 'EPSG:4326'); newfeatures.push(clone); }); //console.log(newfeatures); var string = new ol.format.KML().writeFeatures(newfeatures); //console.log(string); 

我收到错误“未捕获的TypeError:无法读取未定义的属性'length'”

当我控制台变量newfeatures时,我将所有绘制的特征放入数组中。 请帮我解决这个问题

您可以导出功能,而无需手动克隆和转换它们。 将您的整个代码替换为

var features = layer.getSource().getFeatures();
var string = new format.KML().writeFeatures(features, {
  featureProjection: map.getView().getProjection()
});

上面的代码片段假设map变量保存了您的ol.Map实例。

请注意,有没有maxDepthinternalProjectionexternalProjection上的选项ol.format.KML

使用3857投影编写要素

function GetKMLFromFeatures(features) {
    var format = new ol.format.KML();
    var kml = format.writeFeatures(features, {featureProjection: 'EPSG:3857'});
    return kml;
}

直接的KML转换无效。 因此,第一个功能转换为GEOJSON。 然后,使用下面的库将此GEOJSON转换为kml。

https://github.com/mapbox/tokml

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM