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