宣传单将图片叠加在地图下方

时间:2017-10-18 11:09:46

标签: javascript leaflet mapbox

我的传单项目中有一个mapbox地图和一个Image Overlay。我需要将图像放在地图下面(具有透明区域),但我也尝试使用bringToBack()而没有运气。

这是代码:

mymap = new L.Map('map').setView([41.69906, 12.39258],5);

    L.tileLayer('https://api.mapbox.com/styles/v1/.....',
    {zIndex:90}).addTo(mymap);

var bounds = new L.LatLngBounds (
  new L.LatLng(30,-10),
  new L.LatLng(50,36));
mymap.fitBounds(bounds);


var overlay = new L.ImageOverlay("image.png" ,
  bounds, {  
  attribution: "E.U Copernicus Marine Environment Monitoring Service"
});
mymap.addLayer(overlay);
overlay.bringToBack();

传单文档允许将toToBack带到ImageOverlay,但我认为叠加层和地图有2个不同的堆栈。

1 个答案:

答案 0 :(得分:6)

堆叠顺序由map panes控制。您可以添加一个窗格,将其z-index设置为在图块层下,然后将图像叠加层添加到此窗格中。

这样的东西
mymap.createPane('imagebg');
mymap.getPane('imagebg').style.zIndex = 50;

// ...

var overlay = new L.ImageOverlay("image.png" ,
  bounds, {  
  attribution: "E.U Copernicus Marine Environment Monitoring Service",
  pane: 'imagebg'
});

一个演示(半透明瓷砖背后的传单标识)

var map = new L.Map('map').setView([41.69906, 12.39258],5);

map.createPane('imagebg');
map.getPane('imagebg').style.zIndex = 50;

var attributions =  {attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
        '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
        'Imagery © <a href="http://mapbox.com">Mapbox</a>'};

var tiles = L.tileLayer('https://api.tiles.mapbox.com/v4/mapbox.light/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw',attributions).addTo(map);
tiles.setOpacity(0.7);

var bounds = new L.LatLngBounds (
  new L.LatLng(30,-10),
  new L.LatLng(50,36));
map.fitBounds(bounds);


var overlay = new L.ImageOverlay("http://leafletjs.com/docs/images/logo.png" ,
  bounds, {  
  attribution: "Leaflet",
  pane: 'imagebg'
});
map.addLayer(overlay);
html, body {
  height: 100%;
  margin: 0;
}
#map {
  width: 100%;
  height: 100%;
}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" integrity="sha512-M2wvCLH6DSRazYeZRIm1JnYyh22purTM+FDB5CsyxtQJYeKq83arPe5wgbNmcFXGqiSH2XR8dT/fJISVA1r/zQ==" crossorigin=""/>
    <script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js" integrity="sha512-lInM/apFSqyy1o6s89K4iQUKg6ppXEgsVxT35HbzUupEVRh2Eu9Wdl4tHj7dZO0s1uvplcYGmt3498TtHq+log==" crossorigin=""></script>

<div id='map'></div>