我在附加到openlayers3地图的featureOverlay上绘制多边形(投影EPSG:3857),如下所示:
// create openlayers3 map
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.MapQuest({layer: 'sat'})
})
],
target: 'map',
view: new ol.View({
center: [-11000000, 4600000],
zoom: 4
})
});
// create the featureOverlay to receive polygons
var featureOverlay = new ol.FeatureOverlay();
featureOverlay.setMap(map);
// create the tool to draw polygons
draw = new ol.interaction.Draw({
features: featureOverlay.getFeatures(),
type: 'Polygon'
});
map.addInteraction(draw);
直到这里,一切都好!这个想法来自官方Draw features example。下一步是使用投影EPSG:4326将地图上绘制的要素导出到KML文件。我试图通过以下代码完成此任务:
$('#download-button').click(function() {
// get the features drawn on the map
var features = featureOverlay.getFeatures().getArray();
// create an object to write features on a output KML file
var format = new ol.format.KML();
// write features to KML format using projection EPSG:4326
var kml = format.writeFeatures(features, 'EPSG:4326', 'EPSG:3857');
// Save KML node as KML file using FileSaver.js script
var str = (new XMLSerializer).serializeToString(kml);
var blob = new Blob([str], {type: "text/plain;charset=utf-8;"});
saveAs(blob, "NovaCamada.kml");
});
主要问题是KML文件结果总是包含在度量单位上具有顶点的要素(我需要度数单位!),即使在EPSG上显示KML文件时:4326。 最糟糕的(以及我的问题的目的)是即使我更改了将功能写入kml格式的代码片段:
var kml = format.writeFeatures(features);
或:
var kml = format.writeFeatures(features, 'EPSG:3857', 'EPSG:4326');
结果是一样的,没有任何改变!
我希望ol.format.KML类将EPSG:3857的功能转换为EPSG:4326。我是正确的吗? 在格式化之前,我是否需要对功能进行任何重新投影?有人可以帮我完成这项任务吗?
我清楚了吗?
P.S。:FileSaver.js
答案 0 :(得分:3)
writeFeatures
作为第二个参数,具有两个属性的对象文字:dataProjection
和featureProjection
。
KML格式的默认值为dataProjection
,EPSG:4326。这意味着您只需要在您的情况下指定featureProjection
:
var result = kmlFormat.writeFeatures(features, {featureProjection: 'EPSG:3857'});
无关注:我们正在更改OpenLayers 3.1.0中writeFeatures
的返回类型。对于任何格式,writeFeatures
将始终返回一个字符串。这意味着您无需使用writeFeatures
序列化XMLSerializer
自己的结果。