解决方案,感谢
IvanSanchez
,是将启动层的函数引用到变量。然后,可以在不透明度更改函数中引用该变量。扩展原始的传单JS示例(
http://leafletjs.com/examples/extending/extending-2-layers.html
),完整代码如下:
HTML:
<input id="slide" type="range" min="0" max="1" step="0.1" value="1" onchange="updateOpacity(this.value)">
JS公司:
CanvasCircles = L.GridLayer.extend({
createTile: function(coords) {
var tile = document.createElement('canvas');
var tileSize = this.getTileSize();
tile.setAttribute('width', tileSize.x);
tile.setAttribute('height', tileSize.y);
var ctx = tile.getContext('2d');
// Draw whatever is needed in the canvas context
// For example, circles which get bigger as we zoom in
ctx.arc(tileSize.x / 2, tileSize.x / 2, 4 + coords.z * 4, 0, 2 * Math.PI, false);
ctx.fill();
return tile;
}
});
canvasCircles = function(opts) {
return new CanvasCircles(opts);
};
var canvasCirclesLayer = canvasCircles(); //This is what makes the difference!
map.addLayer(canvasCirclesLayer);
function updateOpacity(value) {
canvasCirclesLayer.setOpacity(value);
}