当前位置: 代码迷 >> 综合 >> leaflet 卷帘效果实现
  详细解决方案

leaflet 卷帘效果实现

热度:95   发布时间:2023-10-17 07:50:24.0
import L from 'leaflet';
import 'leaflet/dist/leaflet.css';
import "leaflet-side-by-side";
import config from "./config";let userconfig = {};
//左侧地图
const map = L.map("Map", {
attributionControl: false
}).setView(config.mapInitParams.center, config.mapInitParams.zoom);
//创建底图切换数据源
const baseLayer1 = L.tileLayer(config.baseMaps[0].Url,);//OSM街道图
const baseLayer2 = L.tileLayer(config.baseMaps[1].Url);//ArcGIS影像图
map.addLayer(baseLayer1);//左侧默认卷帘图层
map.addLayer(baseLayer2);//右侧默认卷帘图层
userconfig.leftLayers = [baseLayer1];
userconfig.rightLayers = [baseLayer2];
//卷帘地图效果
userconfig.sideBySide = L.control
.sideBySide(userconfig.leftLayers, userconfig.rightLayers)
.addTo(map);
//左侧下拉框改变事件
document.getElementById("selectLeftV").onchange =function(){
sideBySideChange();
}
//右侧下拉框改变事件
document.getElementById("selectRightV").onchange =function(){
sideBySideChange();
}
function sideBySideChange(){
//这个插件的左右两侧底图不能一样,否则同时只能显示一个,算是一个bug?
var leftvalue = document.getElementById("selectLeftV").value;
var rightvalue = document.getElementById("selectRightV").value;
var LeftLayer = leftvalue === "0" ? baseLayer1 : baseLayer2;
var RightLayer = rightvalue === "0" ? baseLayer2 : baseLayer1;
addLRLayers(LeftLayer,RightLayer);
userconfig.sideBySide.setLeftLayers(userconfig.leftLayers);
userconfig.sideBySide.setRightLayers(userconfig.rightLayers);
}

leaflet 卷帘效果实现