2023-06-09 18:50:47 +08:00
|
|
|
<template>
|
2023-06-14 20:05:12 +08:00
|
|
|
<div ref="mapPopupRef" class="popover">
|
2023-06-09 18:50:47 +08:00
|
|
|
这是弹窗
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
<script>
|
2023-06-15 19:43:58 +08:00
|
|
|
import { Vector as VectorLayer } from 'ol/layer'
|
|
|
|
|
import VectorSource from 'ol/source/Vector'
|
|
|
|
|
import Feature from 'ol/Feature'
|
|
|
|
|
import { Fill, Icon, Stroke, Style } from 'ol/style'
|
|
|
|
|
import { Point } from 'ol/geom'
|
2023-06-09 18:50:47 +08:00
|
|
|
import Overlay from 'ol/Overlay'
|
2023-06-15 19:43:58 +08:00
|
|
|
|
|
|
|
|
import { MarkerIcon } from './markerEnum'
|
|
|
|
|
import { fromLonLat } from 'ol/proj'
|
2023-06-09 18:50:47 +08:00
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
props: {
|
|
|
|
|
list: {
|
|
|
|
|
type: Array,
|
|
|
|
|
required: true
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-06-14 20:05:12 +08:00
|
|
|
data() {
|
|
|
|
|
return {
|
|
|
|
|
currStationInfo: {}
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-06-09 18:50:47 +08:00
|
|
|
mounted() {
|
|
|
|
|
this.map = this.$parent.getMapInstance()
|
2023-06-15 19:43:58 +08:00
|
|
|
this.initLayer()
|
|
|
|
|
this.initMarkers()
|
|
|
|
|
|
2023-06-09 18:50:47 +08:00
|
|
|
this.initMapClick()
|
|
|
|
|
this.initMapPopup()
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
2023-06-15 19:43:58 +08:00
|
|
|
initLayer() {
|
|
|
|
|
this.markerLayer = new VectorLayer({
|
|
|
|
|
source: new VectorSource({
|
|
|
|
|
features: []
|
|
|
|
|
}),
|
|
|
|
|
properties: { name: 'eventMarker' }
|
|
|
|
|
})
|
|
|
|
|
this.map.addLayer(this.markerLayer)
|
|
|
|
|
console.log('%c [ ]-46', 'font-size:13px; background:pink; color:#bf2c9f;', this.markerLayer)
|
|
|
|
|
},
|
|
|
|
|
|
2023-06-09 18:50:47 +08:00
|
|
|
// 初始化marker
|
|
|
|
|
initMarkers() {
|
2023-06-15 19:43:58 +08:00
|
|
|
const markerFeatures = []
|
2023-06-14 20:05:12 +08:00
|
|
|
this.list.forEach(eventItem => {
|
2023-06-15 19:43:58 +08:00
|
|
|
markerFeatures.push(this.getMarker(eventItem))
|
2023-06-09 18:50:47 +08:00
|
|
|
})
|
2023-06-15 19:43:58 +08:00
|
|
|
this.markerLayer.getSource().addFeatures(markerFeatures)
|
2023-06-09 18:50:47 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 初始化地图点击事件
|
|
|
|
|
initMapClick() {
|
2023-06-15 19:43:58 +08:00
|
|
|
this.map.on('click', evt => {
|
|
|
|
|
const feature = this.map.forEachFeatureAtPixel(evt.pixel, feature => {
|
|
|
|
|
return feature
|
|
|
|
|
})
|
|
|
|
|
const stationInfo = feature && feature.values_ && feature.values_.stationInfo
|
|
|
|
|
if (stationInfo) {
|
|
|
|
|
this.showMapPopup(stationInfo)
|
|
|
|
|
} else {
|
|
|
|
|
this.closeMapPopup()
|
|
|
|
|
}
|
2023-06-09 18:50:47 +08:00
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 初始化地图弹窗
|
|
|
|
|
initMapPopup() {
|
|
|
|
|
this.popupOverlay = new Overlay({
|
|
|
|
|
element: this.$refs.mapPopupRef,
|
|
|
|
|
autoPan: true,
|
|
|
|
|
autoPanAnimation: {
|
|
|
|
|
duration: 250
|
2023-06-14 20:05:12 +08:00
|
|
|
},
|
2023-06-15 19:43:58 +08:00
|
|
|
positioning: 'top-center'
|
2023-06-09 18:50:47 +08:00
|
|
|
})
|
|
|
|
|
this.map.addOverlay(this.popupOverlay)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 显示地图弹窗
|
|
|
|
|
showMapPopup(stationInfo) {
|
2023-06-15 19:43:58 +08:00
|
|
|
this.popupOverlay.setPosition(fromLonLat([stationInfo.lon, stationInfo.lat]))
|
2023-06-14 20:05:12 +08:00
|
|
|
this.currStationInfo = stationInfo // 填充基本信息
|
2023-06-09 18:50:47 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
//关闭地图弹窗
|
|
|
|
|
closeMapPopup() {
|
|
|
|
|
this.popupOverlay.setPosition(null)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 获取marker图标
|
|
|
|
|
getMarker(stationInfo) {
|
|
|
|
|
const { lon, lat } = stationInfo
|
2023-06-15 19:43:58 +08:00
|
|
|
const markerFeature = new Feature({
|
|
|
|
|
geometry: new Point(fromLonLat([lon, lat])),
|
|
|
|
|
stationInfo
|
2023-06-09 18:50:47 +08:00
|
|
|
})
|
2023-06-15 19:43:58 +08:00
|
|
|
|
|
|
|
|
markerFeature.setStyle(this.getMarkerStyle(stationInfo.stationType))
|
|
|
|
|
return markerFeature
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 获取marker样式
|
|
|
|
|
getMarkerStyle(type) {
|
|
|
|
|
const src = MarkerIcon[type]
|
|
|
|
|
return new Style({
|
|
|
|
|
image: new Icon({
|
|
|
|
|
src,
|
|
|
|
|
scale: 0.8
|
|
|
|
|
})
|
2023-06-09 18:50:47 +08:00
|
|
|
})
|
|
|
|
|
},
|
2023-06-14 20:05:12 +08:00
|
|
|
|
|
|
|
|
// 设置marker位置
|
|
|
|
|
setMarkerPosition(markerId, position) {
|
|
|
|
|
const overlay = this.map.getOverlayById(markerId)
|
|
|
|
|
overlay.setPosition(position)
|
2023-06-09 18:50:47 +08:00
|
|
|
}
|
2023-06-15 19:43:58 +08:00
|
|
|
},
|
|
|
|
|
watch: {
|
|
|
|
|
list() {
|
|
|
|
|
this.markerLayer.getSource().clear()
|
|
|
|
|
console.log('%c [ ]-122', 'font-size:13px; background:pink; color:#bf2c9f;', this.map)
|
|
|
|
|
this.initMarkers()
|
|
|
|
|
}
|
2023-06-09 18:50:47 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
2023-06-14 20:05:12 +08:00
|
|
|
<style lang="less" scoped>
|
|
|
|
|
.popover {
|
|
|
|
|
width: 200px;
|
|
|
|
|
height: 300px;
|
|
|
|
|
background-color: rgba(2, 26, 29, 0.9);
|
|
|
|
|
box-shadow: 0 0 5px rgba(2, 26, 29, 0.9);
|
|
|
|
|
}
|
|
|
|
|
</style>
|