xjsaas-ui/src/components/map/fssm-map.vue

228 lines
5.8 KiB
Vue
Raw Normal View History

2024-10-15 13:14:38 +08:00
<!--
* @Author: SunTao 328867980@qq.com
* @Date: 2024-10-14 10:46:23
* @LastEditors: SunTao 328867980@qq.com
* @LastEditTime: 2024-10-15 09:41:43
* @FilePath: \znxjxt-ui\src\views\xj\inspection\task-management\components\fssm-map.vue
* @Description: 公共地图
-->
<template>
<div class="map-container">
<div ref="container" :id="`map-${mapId}`"></div>
<div ref="mapController" v-if="showZoom" class="control-container">
<div class="bigButton">
<i class="el-icon-plus" @click="changeZoom(1, 0.5)"></i>
</div>
<div class="smallButton">
<i class="el-icon-minus" @click="changeZoom(-1, 0.5)"></i>
</div>
</div>
</div>
</template>
<script>
import { Map, View } from "ol";
import XYZ from "ol/source/XYZ";
import { Tile as TileLayer } from "ol/layer";
import { defaults as defaultControls } from "ol/control";
export default {
name: "FssmMap",
props: {
// 接收传过来得中心点
centerPoint: {
type: Object,
default: () => {
return { lat: 123.30297096718999, lon: 41.87942945541742 };
},
},
// 接受传过来得地图层级
zoom: {
type: String,
default: "10",
},
// 是否显示地图放大缩小
showZoom: {
type: Boolean,
default: true,
},
mapId: {
type: String,
default: "0",
},
},
data() {
return {
// 地图实例
instance: new Map(),
};
},
mounted() {
this.initMap();
},
methods: {
/* 初始化openlayer地图 */
initMap() {
const map = new Map({
target: `map-${this.mapId}`,
controls: defaultControls({
zoom: false,
attribution: false,
rotate: false,
}),
view: new View({
center: [this.centerPoint.lat, this.centerPoint.lon], //中心点经纬度
zoom: this.zoom, //图层缩放大小
projection: "EPSG:4326",
}),
layers: [
new TileLayer({
source: new XYZ({
url: "http://t{0-7}.tianditu.com/DataServer?x={x}&y={y}&l={z}&T=vec_c&tk=c691040443c68cda625755c5c3e2acc3",
projection: "EPSG:4326",
}),
}),
new TileLayer({
source: new XYZ({
url: "http://t{0-7}.tianditu.com/DataServer?x={x}&y={y}&l={z}&T=cva_c&tk=c691040443c68cda625755c5c3e2acc3",
projection: "EPSG:4326",
}),
}),
],
});
// 图层点击事件
map.on("singleclick", (e) => {
const featureClick = map.forEachFeatureAtPixel(
map.getEventPixel(e.originalEvent),
(feature) => {
return feature;
}
);
// 如果有图层则返回图层点击
if (featureClick) {
this.$emit("feature-click", featureClick);
} else {
// 没有图层则返回地图点击事件
this.$emit("map-click", e);
}
});
// 图层双击事件
map.on("dblclick", (e) => {
const featureDblclick = map.forEachFeatureAtPixel(
map.getEventPixel(e.originalEvent),
(feature) => {
return feature;
}
);
if (featureDblclick) {
this.$emit("feature-dblclick", featureDblclick);
}
});
// 鼠标移入事件
map.on("pointermove", (e) => {
const feature = map.forEachFeatureAtPixel(
map.getEventPixel(e.originalEvent),
(mapFeature) => {
return mapFeature;
}
);
// 线、面要素不做鼠标移入样式修改
if (feature) {
if (feature.getGeometry()?.getType() === "Point") {
map.getTargetElement().style.cursor = "pointer";
this.$emit("pointer-move", feature);
} else {
map.getTargetElement().style.cursor = "auto";
}
} else {
map.getTargetElement().style.cursor = "auto";
}
});
this.instance.set("map", map);
this.instance.set("overlay-list", []);
},
/**
* @description: 切换当前位置
* @param {Array} position 中心点位置
* @param {number} zoom 地图缩放等级
* @param {number} rotation 地图旋转角度
* @param {number} duration 地图垂直深度
* @return
*/
changePosition(position, zoom, rotation, duration) {
const map = this.instance.get("map");
if (map) {
map.getView().animate({
center: position, // 地图中心点位
zoom: zoom || 12, // 地图缩放等级
rotation: rotation || undefined, // 旋转角度
duration: duration || 1000, // 动画加载时间
});
}
},
/**
* @description: 清空地图图层
* @return
*/
clearMapFeature() {
const map = this.instance.get("map");
const [layer] = map.getAllLayers().filter((item) => item.get("type"));
map.removeLayer(layer);
},
/**
* @description: 调整地图缩放等级
* @return
*/
changeZoom(type = 1, zoomStep) {
const map = this.instance.get("map");
if (map) {
const step = zoomStep || 0.5;
// 获取当前地图缩放等级
const currentZoom = map.getView().getZoom();
map.getView().animate({
zoom: currentZoom + step * type,
});
}
},
/* 生命周期函数 */
beforeDestroy() {
this.instance.clear();
},
},
};
</script>
<style lang="scss" scoped>
.map-container {
width: 100%;
height: 100%;
#map-0 {
width: 100%;
height: 100%;
}
#map-1 {
width: 100%;
height: 100%;
}
.control-container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
position: absolute;
left: 1rem;
top: 1rem;
i {
padding: 0.1rem 0;
font-size: 1.2rem;
}
}
}
</style>