预览效果
前期准备
- leaflet中文网,下载css和js,我这里还用到两个插件,一个是用来改颜色,一个是右键菜单
- 什么是瓦片地图?简而言之就是一堆图片组合,所以可以使用css对图片的操作
- 瓦片地图下载: https://blog.csdn.net/m0_37942304/article/details/131434276
其实也可以用原生的高德地图api离线版,不过很难弄到,操作也很有限
<link rel="stylesheet" href="./leaflet/leaflet.css" />
<link rel="stylesheet" href="./leaflet/leaflet.contextmenu.min.css" />
<script src="./leaflet/leaflet.js"></script>
<script src="./leaflet/leaflet-tilelayer-colorizr.js"></script>
具体代码
注意事项
- 不清楚leaflet的具体实现,直接使用路径存在一定问题,只能用导入图片的方式,并且nextTick也是必须的
import greenCir from '@/assets/gif/breath_green.gif'
import red1 from '@/assets/gif/breath_red.gif'
async initMap() {
this.$nextTick(() => {
var corner1 = L.latLng(0, 60)
var corner2 = L.latLng(55, 140)
var bounds = L.latLngBounds(corner1, corner2)
this.map = L.map('map', {
center: [40.02404009136253, 116.50641060224784],
zoom: 4,
zoomControl: false,
attributionControl: false,
maxBounds: bounds,
})
L.tileLayer
.colorizr('/map/{z}/{x}/{y}.png', {
minZoom: 4,
maxZoom: 13,
attribution: '<b style="color:#40a9ff">高德地图</b>',
colorize: function (pixel) {
const { r, g, b } = pixel
if ((r == 163, g == 204, b == 255)) {
pixel.r = 252
pixel.g = 209
pixel.b = 150
}
return pixel
},
})
.addTo(this.map)
this.map
.on('contextmenu', (e) => {
this.map.setView([104.1017375, 30.3809537], 4)
})
.on('click', () => {
console.log('info', this.info)
this.info = ''
})
this.drawCircle()
})
},
drawCircle() {
this.layerGroup?.clearLayers()
const greenIcon = L.icon({
iconUrl: greenCir,
iconSize: [25, 25],
})
const redIcon = L.icon({
iconUrl: red1,
iconSize: [25, 25],
})
console.log('点数据', this.data)
this.markers = []
this.data[0].forEach((item) => {
if (item.position?.length !== 2) return
console.log('点位置', item.position)
let icon
if (item.alarm_device_list.length) {
icon = redIcon
} else {
icon = greenIcon
}
const marker = L.marker(
{
lng: item.position[0],
lat: item.position[1],
},
{
icon: icon,
title: item.name,
}
)
marker.on('click', () => {
this.info = item.name + '告警数量:' + item.alarm_device_list.length
this.map.setView(
{
lng: item.position[0],
lat: item.position[1],
},
13
)
})
marker.on('contextmenu', (e) => {
const timer = setTimeout(() => {
if (item.alarm_device_list.length) {
this.$router.push('/device/' + item.alarm_device_list[0])
} else {
this.$router.push('/device/' + item.device_list[0])
}
clearTimeout(timer)
}, 100)
})
this.markers.push(marker)
})
this.layerGroup = L.layerGroup(this.markers)
this.map.addLayer(this.layerGroup)
},
- 改变样式主要是通过前面插件对rgb的修改和这里的filter过滤
.leaflet-zoom-animated img {
-webkit-filter:invert(1) saturate(0.4) brightness(1.6) !important;
-moz-filter:invert(1) saturate(0.4) brightness(1.6) !important;
filter: invert(1) saturate(0.4) brightness(1.6) !important;
}