AnalysisSystemForRadionucli.../src/views/stationOperation/components/Map.vue

131 lines
2.4 KiB
Vue
Raw Normal View History

<template>
<div ref="mapContainerRef" class="map-container">
<template v-if="map">
<slot></slot>
</template>
</div>
</template>
<script>
import TileLayer from 'ol/layer/Tile'
import Map from 'ol/Map'
import XYZ from 'ol/source/XYZ'
import View from 'ol/View'
import { fromLonLat } from 'ol/proj'
2023-12-14 18:30:49 +08:00
const mapSourceUrl = process.env.VUE_APP_MAP_BASE_URL
export default {
props: {
zoom: {
type: Number,
2023-12-14 18:30:49 +08:00
default: 1,
},
maxZoom: {
type: Number,
2023-12-14 18:30:49 +08:00
default: 16,
},
minZoom: {
type: Number,
2023-12-14 18:30:49 +08:00
default: 1,
},
center: {
type: Object,
default() {
return {
longitude: 116,
2023-12-14 18:30:49 +08:00
latitude: 40,
}
2023-12-14 18:30:49 +08:00
},
},
},
data() {
return {
map: null,
2023-12-14 18:30:49 +08:00
stationList: [],
}
},
mounted() {
this.initMap()
},
methods: {
// 初始化地图
initMap() {
const { longitude, latitude } = this.center
2023-12-14 18:30:49 +08:00
this.tileLayer = new TileLayer({
source: new XYZ({
url: mapSourceUrl,
}),
})
const layers = [this.tileLayer]
const view = new View({
projection: 'EPSG:3857', // 使用这个坐标系
2023-12-14 18:30:49 +08:00
center: fromLonLat([longitude, latitude]),
zoom: this.zoom,
maxZoom: this.maxZoom,
2023-12-14 18:30:49 +08:00
minZoom: this.minZoom,
})
this.map = new Map({
target: this.$refs.mapContainerRef,
layers,
view,
2023-12-14 18:30:49 +08:00
controls: [],
})
},
/**
* 以下是工具方法
*/
// 获取地图缩放级别
getZoom() {
return this.map.getView().getZoom()
},
// 设置地图缩放级别
setZoom(zoom) {
if (zoom < this.minZoom) {
zoom = this.minZoom
}
if (zoom > this.maxZoom) {
zoom = this.maxZoom
}
this.map.getView().animate({ zoom })
},
getMapInstance() {
return this.map
},
// 获取地图中心点
getCenter() {
return this.map.getView().getCenter()
},
// 平移到某个位置
panTo(center, duration = 1000) {
return this.map.getView().animate({
center: fromLonLat(center),
2023-12-14 18:30:49 +08:00
duration,
})
2023-12-14 18:30:49 +08:00
},
// 修改地图来源
changeSource(url) {
this.tileLayer.setSource(
new XYZ({
url,
})
)
},
},
}
</script>
<style lang="less" scoped>
.map-container {
height: 100%;
position: relative;
}
</style>