NEZ-3322 fix:优化 map占用的内存大小
This commit is contained in:
@@ -82,7 +82,7 @@ if (arg === 'html') {
|
|||||||
compress: {
|
compress: {
|
||||||
warnings: false,
|
warnings: false,
|
||||||
drop_console: true,
|
drop_console: true,
|
||||||
pure_funcs: ['console.log']
|
pure_funcs: ['console.log', 'console.error']
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
exclude: /manifest.+js/,
|
exclude: /manifest.+js/,
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ import axios from 'axios'
|
|||||||
import 'leaflet/dist/leaflet.css'
|
import 'leaflet/dist/leaflet.css'
|
||||||
import maplibregl from 'maplibre-gl'
|
import maplibregl from 'maplibre-gl'
|
||||||
import mapAllStyle from './mapStyle'
|
import mapAllStyle from './mapStyle'
|
||||||
|
import { getChart, setChart, chartCache } from '@/components/common/js/common'
|
||||||
const regNum = /^[0-9]+.?[0-9]*/
|
const regNum = /^[0-9]+.?[0-9]*/
|
||||||
export default {
|
export default {
|
||||||
name: 'chartMap',
|
name: 'chartMap',
|
||||||
@@ -69,7 +70,7 @@ export default {
|
|||||||
data () {
|
data () {
|
||||||
const theme = localStorage.getItem(`nz-user-${localStorage.getItem('nz-user-id')}-theme`) || 'light'
|
const theme = localStorage.getItem(`nz-user-${localStorage.getItem('nz-user-id')}-theme`) || 'light'
|
||||||
return {
|
return {
|
||||||
map: null,
|
mapId: null,
|
||||||
tooltip: {
|
tooltip: {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
@@ -116,12 +117,13 @@ export default {
|
|||||||
const mapConfig = JSON.parse(response.data.paramKey.map_center_config)
|
const mapConfig = JSON.parse(response.data.paramKey.map_center_config)
|
||||||
mapStyle.center = [Number(mapConfig.longitude), Number(mapConfig.latitude)]
|
mapStyle.center = [Number(mapConfig.longitude), Number(mapConfig.latitude)]
|
||||||
mapStyle.zoom = Number(mapConfig.zoom)
|
mapStyle.zoom = Number(mapConfig.zoom)
|
||||||
if (this.map) {
|
const mapId = this.mapId = this.isFullscreen ? document.getElementById('map-screen-' + this.chartInfo.id) : document.getElementById('map' + this.chartInfo.id)
|
||||||
this.map.remove()
|
let map = getChart(mapId) || null
|
||||||
this.map = null
|
if (map) {
|
||||||
|
map.remove()
|
||||||
|
map = null
|
||||||
}
|
}
|
||||||
const mapId = this.isFullscreen ? document.getElementById('map-screen-' + this.chartInfo.id) : document.getElementById('map' + this.chartInfo.id)
|
map = new maplibregl.Map({
|
||||||
this.map = new maplibregl.Map({
|
|
||||||
container: mapId,
|
container: mapId,
|
||||||
style: mapStyle,
|
style: mapStyle,
|
||||||
maxZoom: mapConfig.maxZoom || 7,
|
maxZoom: mapConfig.maxZoom || 7,
|
||||||
@@ -161,7 +163,8 @@ export default {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
this.map.on('load', this.mapLoad)
|
map.on('load', this.mapLoad)
|
||||||
|
setChart(mapId, map)
|
||||||
maplibregl.addProtocol('nzMap', (params, callback) => { // 切片显示接口 防止跨域的问题
|
maplibregl.addProtocol('nzMap', (params, callback) => { // 切片显示接口 防止跨域的问题
|
||||||
fetch(`${params.url.split('://')[1]}`)
|
fetch(`${params.url.split('://')[1]}`)
|
||||||
.then(t => {
|
.then(t => {
|
||||||
@@ -184,13 +187,21 @@ export default {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
mapLoad () {
|
mapLoad () {
|
||||||
|
const map = getChart(this.mapId)
|
||||||
|
if (!map) {
|
||||||
|
return
|
||||||
|
}
|
||||||
this.loadDataCenterMapData()
|
this.loadDataCenterMapData()
|
||||||
this.map.addControl(new maplibregl.NavigationControl(), 'bottom-right')
|
map.addControl(new maplibregl.NavigationControl(), 'bottom-right')
|
||||||
const mapboxglInner = document.getElementsByClassName('mapboxgl-ctrl-attrib-inner')
|
const mapboxglInner = document.getElementsByClassName('mapboxgl-ctrl-attrib-inner')
|
||||||
mapboxglInner[0].innerHTML = '<span>© MapTiler</span> <span>© OpenStreetMap contributors</span>'
|
mapboxglInner[0].innerHTML = '<span>© MapTiler</span> <span>© OpenStreetMap contributors</span>'
|
||||||
},
|
},
|
||||||
loadDataCenterMapData () {
|
loadDataCenterMapData () {
|
||||||
const self = this
|
const self = this
|
||||||
|
const map = getChart(this.mapId)
|
||||||
|
if (!map) {
|
||||||
|
return
|
||||||
|
}
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
const requests = [axios.get('dc?pageSize=-1'), axios.get('/stat/overview/map')]
|
const requests = [axios.get('dc?pageSize=-1'), axios.get('/stat/overview/map')]
|
||||||
axios.all(requests).then(result => {
|
axios.all(requests).then(result => {
|
||||||
@@ -223,8 +234,8 @@ export default {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
this.renderPoint(dcStats)
|
this.renderPoint(dcStats)
|
||||||
self.map.on('mouseenter', 'pointLayer', self.pointEnter)
|
map.on('mouseenter', 'pointLayer', self.pointEnter)
|
||||||
self.map.on('mouseleave', 'pointLayer', self.pointLeave)
|
map.on('mouseleave', 'pointLayer', self.pointLeave)
|
||||||
self.pointAnimation(0)
|
self.pointAnimation(0)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -255,6 +266,10 @@ export default {
|
|||||||
},
|
},
|
||||||
renderPoint (dcStats) {
|
renderPoint (dcStats) {
|
||||||
const arr = []
|
const arr = []
|
||||||
|
const map = getChart(this.mapId)
|
||||||
|
if (!map) {
|
||||||
|
return
|
||||||
|
}
|
||||||
dcStats.forEach(dcStat => {
|
dcStats.forEach(dcStat => {
|
||||||
arr.push({
|
arr.push({
|
||||||
type: 'Feature',
|
type: 'Feature',
|
||||||
@@ -267,14 +282,14 @@ export default {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
this.map.addSource('pointData', {
|
map.addSource('pointData', {
|
||||||
type: 'geojson',
|
type: 'geojson',
|
||||||
data: {
|
data: {
|
||||||
type: 'FeatureCollection',
|
type: 'FeatureCollection',
|
||||||
features: arr
|
features: arr
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
this.map.addLayer({
|
map.addLayer({
|
||||||
id: 'pointLayer',
|
id: 'pointLayer',
|
||||||
type: 'circle',
|
type: 'circle',
|
||||||
source: 'pointData',
|
source: 'pointData',
|
||||||
@@ -302,9 +317,10 @@ export default {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
pointAnimation (timeStep) {
|
pointAnimation (timeStep) {
|
||||||
|
const map = getChart(this.mapId)
|
||||||
const opacity = 0.5 + (timeStep % 1000) / 1000 / 2
|
const opacity = 0.5 + (timeStep % 1000) / 1000 / 2
|
||||||
if (this.map) {
|
if (map) {
|
||||||
this.map.setPaintProperty('pointLayer', 'circle-opacity', [
|
map.setPaintProperty('pointLayer', 'circle-opacity', [
|
||||||
'step',
|
'step',
|
||||||
['get', 'color'],
|
['get', 'color'],
|
||||||
0.5,
|
0.5,
|
||||||
@@ -318,8 +334,12 @@ export default {
|
|||||||
},
|
},
|
||||||
resize () {
|
resize () {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.map && this.map.remove()
|
let map = getChart(this.mapId)
|
||||||
this.map = null
|
if (!map) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
map && map.remove()
|
||||||
|
map = null
|
||||||
this.initChart()
|
this.initChart()
|
||||||
}, 100)
|
}, 100)
|
||||||
}
|
}
|
||||||
@@ -329,11 +349,15 @@ export default {
|
|||||||
clearTimeout(this.timer)
|
clearTimeout(this.timer)
|
||||||
this.timer = null
|
this.timer = null
|
||||||
}
|
}
|
||||||
this.map.off('load', this.mapLoad)
|
let map = getChart(this.mapId)
|
||||||
this.map.off('mouseenter', 'pointLayer', this.pointEnter)
|
if (!map) {
|
||||||
this.map.off('mouseleave', 'pointLayer', this.pointLeave)
|
return
|
||||||
this.map && this.map.remove()
|
}
|
||||||
this.map = null
|
map.off('load', this.mapLoad)
|
||||||
|
map.off('mouseenter', 'pointLayer', this.pointEnter)
|
||||||
|
map.off('mouseleave', 'pointLayer', this.pointLeave)
|
||||||
|
map.remove()
|
||||||
|
map = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user