2017-06-15 66 views
0

添加KML是有任何方式在openlayer 3添加KML矢量这样但KML是从字符串变量的OpenLayers 3如何使用字符串变量

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); 

在此先感谢

回答

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' });

+0

顺便说一句,我不使用来自的OpenLayers的KML。我有一个字符串变量,其中包含字符串它的投影在3857年。和我已经尝试过的代码,但它不工作有没有错误,但kml层不显示 – gray

+0

是否有任何其他方式加载从kml数据库,而不是传递给字符串 – gray

+0

您的问题是如何加载它来自字符串。更好地决定你真正想要什么,这样我们就不会浪费时间。 – pavlos

相关问题