简体   繁体   English

打开第3层:如何仅显示KML层

[英]Open Layers 3: how to display only a KML layer

i am trying to create an OpenLayers map that only displays a KML layer without a base map or underlying tile layer. 我正在尝试创建一个仅显示KML图层而没有基础地图或基础图块图层的OpenLayers地图。

the KML layer will be an indoor floormap, but it does not need to sit on top of an existing map, in a particular coordinate location. KML图层将是一个室内楼层地图,但是它不必位于特定坐标位置的现有地图之上。 I just need to display the floormap on its own, with no other map. 我只需要单独显示楼层地图,而无需显示其他地图。 i will also want to set pan limits so the user can not pan away from the map. 我还将要设置平移限制,以使用户无法平移地图。

below is some code that i used to successfully display the KML layer on top of an existing base map. 以下是一些我用来在现有基础地图上方成功显示KML层的代码。 I've tried many things to try get the KML layer to display on its own but to no avail. 我已经尝试了很多方法来尝试让KML图层自行显示,但无济于事。

can anyone help with this, or tell me what i need to change with the code below to display the KML on its own? 任何人都可以帮忙解决这个问题,或者告诉我我需要对以下代码进行更改以单独显示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);

thanks! 谢谢!

as Jonatas and Tsauerwein pinted out, i just had to remove the tile layer. 当Jonatas和Tsauerwein退出时,我只需要删除平铺层。

a la:

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

Did you check whether you can read the KML file? 您检查过是否可以读取KML文件吗? There might be a CORS issue. 可能存在CORS问题。

I recommend to use an AJAX call to load the KML and then use ol.format.KML to read the features and add these to the source. 我建议使用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);
    }
  });

If you cannot read the file like this check in the Firebugz NET tab for CORS issues. 如果您无法读取此类文件,请在Firebugz NET选项卡中检查CORS问题。 在此处输入图片说明

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

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