使用leaflet绘制地图,地图包含站点
创建一个容器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16const map = L.map("map", {
center: [41.486904, 122.621087], // 中心lo&la
zoom: 7.25, // 缩放级别
zoomControl: false, // 是否将zoom控件放入图中
minZoom: 7,
maxZoom: 11,
zoomSnap: 0.25, // 缩放级别倍数,默认接近整数,这里允许更大颗粒度
scrollWheelZoom: false, // can scroll everywhere
smoothWheelZoom: true, // enable smooth zoom ?这个文档里没找到
smoothSensitivity: 0.4, // zoom speed. default is 1 同上
attributionControl: false, // 是否显示归属版权
maxBounds: [ // 指定边界,超出弹回
[47.709761542666, 139.81201171875],
[33.063924198121, 104.21630859375],
],
});创建栅格图层
const BaseLayer = L.tileLayer(url)
将栅格图层放入容器
BaseLsyer.addTo(map)
根据地图文件创建地图对象,放入容器
L.geoJson(json数据, {style: ()⇒{}, onEachFeature: 绑定事件回调}).addTo(map)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49/* 部分事件回调 */
// 高亮图层事件
const highlightFeature = (e) => {
if (this.shadowLayer) {
this.map.removeLayer(this.shadowLayer);
}
this.city = e.target.feature.properties.name;
// 阴影层
this.shadowLayer = L.geoJson(e.target.toGeoJSON(), {
style: {
fillColor: "#f2f9ff",
fillOpacity: 0.4,
color: "#f2f9ff",
weight: 0,
},
}).addTo(this.map);
let obj = this.shadowLayer._layers;
const values = Object.values(obj);
const container = values[0]._path;
container.style.transform = "translateY(10px)"; // 使立体
this.shadowLayer.bringToFront();
// 高亮层
let originalLayer = e.target;
originalLayer.setStyle({
color: "#C2F7FF ", // 边界线颜色
fillColor: "#82E0FF", // 区域填充颜色
fillOpacity: 0.5, // 区域填充的透明度
weight: 2, // 边界线宽度
});
originalLayer.bringToFront();
};
// 祛除高亮事件
const resetHighlight = (e) => {
geojson.resetStyle(e.target); // 还原地图样式
if (this.shadowLayer) { // 删去阴影层
this.map.removeLayer(this.shadowLayer);
}
};
// 鼠标事件
const onEachFeature = (feature, layer) => {
this.cityLayerMap[feature.properties.name] = layer;
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
});
};将需要的图层至于顶层
geoLayer.bringToFront()
根据数据显示merker: 获取数据; 处理数据; 遍历生成marker👇
- 普通marker:
for(){const aIcon = L.icon(iconUrl, className); L.marker([lo, la], {icon: aIcon}).addTo(map)}
- 自定义marker:
....; const icon = L.divIcon({html: 自己拼接的图标模板字符串}); ....
- 普通marker:
给marker绑定监听
marker.on('click/dblclick/....', () => {active-shadow(👇)/open/flyTo/cancelBefore})
- marker高亮
activeMarkerArr.push(marker); document.querySelector(…).classList.toggle(’active);
- 所在地图块高亮
const combineLayer = L.layerGroup([activeLayer, shadowLayer]); map.addLayer(combineLayer)
- 取消marker高亮
toggle('active')
- 取消地图块高亮
map.removeLayer(combineLayer )
- marker高亮
若结合多选框,则需
activeList.…; activeMarkerArr….;
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Ra-Liz's Blog!
评论
ValineGitalk