Openlayers 3如何使用字符串变量添加kml?

时间:2017-06-15 23:42:51

标签: kml openlayers-3

有没有办法在这样的openlayer 3中添加kml矢量,但kml来自字符串变量?

  var vector = new ol.layer.Vector({
            source: new ol.source.Vector({
              url: 'https://openlayers.org/en/v4.2.0/examples/data/kml/2012-02-10.kml',
              format: new ol.format.KML()
            })
          });

我试过这段代码,但它没有用。

var kmlString = result;
var features = new ol.format.KML().readFeatures(kmlString);

var KMLvectorSource = new ol.source.Vector({
    features: features
});


var KMLvector = new ol.layer.Vector({ source: KMLvectorSource });

//KMLvector.addFeatures(features);
map.addLayer(KMLvector);

提前致谢。

1 个答案:

答案 0 :(得分:0)

您缺少kml功能的投影定义和转换。 https://openlayers.org/en/v4.2.0/examples/data/kml/2012-02-10.kml中存在的kml要素投影在EPSG:4326,而您的地图投影在EPSG:3857中。对于kml,通常会在4326中进行投影,因此在大多数情况下,kml的EPSG代码应为4326.

现在回到你的问题。改变这一行:

var features = new ol.format.KML().readFeatures(kmlString);

对此:

var features = new ol.format.KML().readFeatures(kmlString,{ dataProjection:'EPSG:4326', featureProjection:'EPSG:3857' });