我有一些问题需要理解传单中的图层排序是如何工作的,而且我对Javascript / Coffeescript完全不熟悉。
我正在处理的代码是Ruby& Rails应用程序的一部分,可以找到here。想法是
问题:缩放导致重新绘制图层。因此,用户选择矩形最终位于热图的透明层下。我希望它能保持领先地位。我尝试应用featuregroup.bringToBack()和featuregroup.bringToFront()m但它没有按预期工作。
我为这个stackoverflow问题写了一些example code with jsfiddle来以更简单的方式说明问题和我的方法。您需要leaflet.js,leaflet.css和leaflet-heat.js才能实现此目的:
# prepare basemap
osmUrl = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png'
osmAttrib = '© <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors'
osm = L.tileLayer(osmUrl,
maxZoom: 18
attribution: osmAttrib)
# initialize the map with basemap
map = L.map('map').setView([
17
75
], 5).addLayer(osm)
# coordinate vectors
lat = [14, 15, 16, 18, 19, 20]
lng = [72, 73, 74, 76, 77, 78]
# function: layer arrangement in relation to zoom state
arrange_layers = ->
if map.getZoom() > 5
console.log "1"
heatItem.bringToFront()
if map.getZoom() <= 5
console.log "2"
heatItem.bringToBack()
return
#function: react to user zooming
map.on 'zoomend', (e) ->
arrange_layers()
console.log map.getZoom();
return
# add circles to map
for lati in lat
for lngi in lng
L.circle([
lati
lngi
], 50000,
color: 'black'
fillColor: '#black'
fillOpacity: 0.5).addTo(map)
# add heatmap to map
heatItem = new (L.FeatureGroup)
heat = L.heatLayer([
[
15
73
3000
]
[
19
77
10000
]
], radius: 25).addTo(heatItem)
heatItem.addTo(map)
修改:
我的问题的解决方案是更改为Leaflet 1.0并添加一些定义了z值的自定义窗格。现在我可以控制图层顺序了。更正后的示例代码如下所示,取决于leaflet.js,leaflet.css和leaflet-heat.js:
# prepare basemap
osmUrl = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png'
osmAttrib = '© <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors'
osm = L.tileLayer(osmUrl,
maxZoom: 18
attribution: osmAttrib)
# initialize the map with basemap
map = L.map('map').setView([
17
75
], 5).addLayer(osm)
# coordinate vectors
lat = [14, 15, 16, 18, 19, 20]
lng = [72, 73, 74, 76, 77, 78]
# create pane
map.createPane 'circles'
map.getPane('circles').style.zIndex = 450
# add circles to map
for lati in lat
for lngi in lng
L.circle([
lati
lngi
], 50000,
color: 'black'
fillColor: '#black'
fillOpacity: 0.5
pane: 'circles').addTo(map)
# add heatmap to map
heatItem = new (L.FeatureGroup)
heat = L.heatLayer([
[
15
73
3000
]
[
19
77
10000
]
], radius: 25).addTo(heatItem)
heatItem.addTo(map)