2015-06-17 75 views
1

我試圖創建一個OpenLayers地圖,該地圖僅顯示沒有底圖或底層圖塊層的KML圖層。打開圖層3:如何僅顯示KML圖層

KML圖層將成爲室內地圖,但它不需要坐在現有地圖的頂部,在特定座標位置。我只需要自己顯示樓層地圖,沒有其他地圖。我也想要設置平移限制,以便用戶不能離開地圖。

下面是我用來在現有底圖上成功顯示KML圖層的一些代碼。我已經嘗試了很多方法來嘗試讓KML圖層獨立顯示,但無濟於事。

任何人都可以幫助解決這個問題,或者告訴我需要改變下面的代碼才能自行顯示KML?

var vector = new ol.layer.Vector({ 
     source: new ol.source.Vector({ 
      url: MAPS_URL + 'map1.kml', 
      format: new ol.format.KML() 
     }) 
    }); 

var map = new ol.Map({ 
      target: 'map', 
      layers: [ 
       new ol.layer.Tile({ 
        source: new ol.source.MapQuest({layer: 'sat'}) 
       }), 
       vector 
      ], 
      view: new ol.View({ 
      center: ol.proj.transform([37.41, 8.82], 'EPSG:4326', 'EPSG:3857'), 
      zoom: 2 
     }) 
    }); 

map.addLayer(vector); 

謝謝!

+1

如果刪除圖塊層會發生什麼? – tsauerwein

+1

我分叉了一個[openlayers kml示例](http://plnkr.co/edit/A5rwSWUenZbB1saJ6Vm6?p=preview),刪除了底圖,一切正常。 –

+0

是的,工作正常。我已經嘗試過,但無法使用它!可能只是一個愚蠢的語法錯誤或什麼,謝謝指出這對我! – ThriceGood

回答

0

您是否檢查您是否可以讀取KML文件?可能存在CORS問題。

我推薦使用AJAX調用來加載KML,然後使用ol.format.KML來讀取這些特性並將它們添加到源代碼中。

sourceVector = new ol.source.Vector(); 
layerVector = new ol.layer.Vector({ 
    source: sourceVector 
    }); 
formatKML = new ol.format.KML({extractStyles: false}); 
$.ajax('http://storage.googleapis.com/dbauszus-file-bucket/rtmLmpHg.kml',{ 
    type: 'GET', 
    contentType: 'application/vnd.google-earth.kml+xml', 
    success : function(response) { 
    features = formatKML.readFeatures(response,{ 
     dataProjection: 'EPSG:4326', 
     featureProjection: 'EPSG:3857' 
    }); 
    sourceVector.addFeatures(features); 
    } 
    }); 

如果您無法閱讀這樣的檢查在Firebugz NET選項卡CORS問題的文件。 enter image description here

1

隨着Jonatas和Tsauerwein拼出來,我只好刪除瓷磚層。

一拉:

var vector = new ol.layer.Vector({ 
    source: new ol.source.Vector({ 
     url: MAPS_URL + 'map1.kml', 
     format: new ol.format.KML() 
    }) 
}); 

var map = new ol.Map({ 
     target: 'map', 
     layers: [vector], 
     view: new ol.View({ 
     center: ol.proj.transform([37.41, 8.82], 'EPSG:4326', 'EPSG:3857'), 
     zoom: 2 
    }) 
}); 

map.addLayer(vector);