如何将图层添加到地图时将其添加为OpenLayers 3中的底层?

时间:2015-08-13 09:14:56

标签: javascript openlayers-3

我试图找出一种切换地图类型并将图层添加为底层的好方法。其他层是始终位于顶部的信息层。我尝试使用map.baseLayer属性,但图层仍然呈现在其他图层之上。

这是我得到的:

var OSMLayer = new ol.layer.Tile({
    source: new ol.source.OSM()
});

// Initial map
var currentMap = OSMLayer;

var mapQuestOSMLayer = new ol.layer.Tile({
    source: new ol.source.MapQuest({layer: 'osm'})
});

var mapQuestSATLayer = new ol.layer.Tile({
    source: new ol.source.MapQuest({layer: 'sat'})
});
var mapQuestHYBLayer = new ol.layer.Tile({
    source: new ol.source.MapQuest({layer: 'hyb'})
});

var map = new ol.Map({
  layers: [OSMLayer],
  target: document.getElementById('map-canvas'),
  view: new ol.View({
    center: [0, 0],
    zoom: 3
  })
});

// How can i set the layer as the bottom layer?

$('#btnMapTypes ul.dropdown-menu li a').click(function (e) {
    map.removeLayer(currentMap);
    switch($(this).attr('data-type')){
        case 'OSM':
            map.addLayer(OSMLayer);
            map.baseLayer = OSMLayer;
            currentMap = OSMLayer;
        break;
        case 'mapQuestOSM':
            map.addLayer(mapQuestOSMLayer);
            map.baseLayer = mapQuestOSMLayer;
            currentMap = mapQuestOSMLayer;
        break;
        case 'mapQuestSAT':
            map.addLayer(mapQuestSATLayer);
            map.baseLayer = mapQuestSATLayer;
            currentMap = mapQuestSATLayer;
        break;
        case 'mapQuestHYB':
            map.addLayer(mapQuestHYBLayer);
            map.baseLayer = mapQuestHYBLayer;
            currentMap = mapQuestHYBLayer;
        break;
    }           
});

实现我想要的结果的正确方法是什么?谢谢!

1 个答案:

答案 0 :(得分:2)

如果你在地图上做getLayers,你会得到一个集合,它具有操纵顺序的功能,例如insertAt(或setAt,具体取决于你的需要):http://openlayers.org/en/v3.8.2/apidoc/ol.Collection.html