ly-front/src/views/mapControl/index.vue

149 lines
3.5 KiB
Vue
Raw Normal View History

2025-03-31 15:26:29 +00:00
<template>
2025-06-16 14:22:14 +00:00
<div class="HomeMap">
2025-04-01 16:12:21 +00:00
<div class="zoom-level">当前缩放等级: {{ zoomLevel }}</div>
2025-06-16 14:22:14 +00:00
<div id="map" ref="olMap" class="map-container"></div>
2025-04-13 08:23:44 +00:00
<div class="pointingNorth" @click="mapNorth">
2025-06-16 14:22:14 +00:00
<img src="@/assets/img/menu/zhibeizhen.png" alt="指北" />
2025-04-13 08:23:44 +00:00
</div>
2025-03-31 15:26:29 +00:00
</div>
</template>
<script>
import { mapGetters } from "vuex";
2025-06-16 14:22:14 +00:00
import Map from "ol/Map";
import View from "ol/View";
import TileLayer from "ol/layer/Tile";
import { fromLonLat } from "ol/proj";
import XYZ from "ol/source/XYZ";
2025-03-31 15:26:29 +00:00
export default {
name: "HomeMap",
data() {
return {
2025-06-16 14:22:14 +00:00
map: null,
2025-04-01 16:12:21 +00:00
zoomLevel: null
2025-03-31 15:26:29 +00:00
};
},
created() {
2025-06-16 14:22:14 +00:00
// 清空控制台(可选)
2025-03-31 15:26:29 +00:00
// console.clear();
},
mounted() {
2025-06-16 14:22:14 +00:00
this.initMap();
2025-03-31 15:26:29 +00:00
},
computed: {
...mapGetters(["statePage", "cityCode"])
},
methods: {
2025-06-16 14:22:14 +00:00
initMap() {
// 初始化 OpenLayers 地图
this.map = new Map({
target: this.$refs.olMap,
layers: [
new TileLayer({
visible: true,
source: new XYZ({
visible: true,
url: "http://webrd01.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=2&scale=1&style=8"
})
})
],
view: new View({
projection: "EPSG:4326",
center: [116.4, 39.9], // 经纬度转投影坐标
zoom: 10,
minZoom: 3,
maxZoom: 18,
constrainRotation: false // 允许旋转
})
2025-03-31 15:26:29 +00:00
});
2025-06-16 14:22:14 +00:00
// 保存地图实例到全局(与 Mars3D 的 window.marsMap 类似)
window.olMap = this.map;
// 处理地图加载完成后的逻辑
this.onMapLoad(this.map);
// 绑定点击事件
this.map.on("click", this.mapClickHandler);
// 绑定缩放/移动结束事件
this.map.getView().on("change:resolution", this.mapZoomHandler);
this.map.getView().on("change:center", this.mapZoomHandler);
2025-03-31 15:26:29 +00:00
},
2025-06-16 14:22:14 +00:00
mapClickHandler(event) {
// 处理地图点击事件
console.log("地图点击:", event);
2025-04-01 16:12:21 +00:00
},
2025-06-16 14:22:14 +00:00
mapZoomHandler() {
// 获取当前缩放等级
this.zoomLevel = Math.round(this.map.getView().getZoom());
2025-04-01 16:12:21 +00:00
},
2025-03-31 15:26:29 +00:00
onMapLoad(map) {
2025-06-16 14:22:14 +00:00
// 地图加载完成后的处理
const camera = window.mapConfig || {};
2025-04-12 15:15:32 +00:00
if (camera.isCamera) {
2025-06-16 14:22:14 +00:00
// 设置视角约束
map
.getView()
.setCenter(
fromLonLat([camera.cameraLon || 0, camera.cameraLat || 0])
);
map.getView().setMinZoom(3); // 最小缩放等级
map.getView().setMaxZoom(18); // 最大缩放等级
2025-04-12 15:15:32 +00:00
}
2025-04-13 08:23:44 +00:00
},
mapNorth() {
2025-06-16 14:22:14 +00:00
// 指北:将地图旋转角度重置为 0
const view = this.map.getView();
const currentCenter = view.getCenter();
const currentZoom = view.getZoom();
view.animate({
center: currentCenter,
zoom: currentZoom < 3 ? 10 : currentZoom, // 确保最小缩放等级
rotation: 0, // 指北
duration: 500 // 动画持续时间
2025-04-13 11:05:15 +00:00
});
2025-03-31 15:26:29 +00:00
}
}
};
</script>
2025-06-16 14:22:14 +00:00
2025-03-31 15:26:29 +00:00
<style scoped lang="scss">
2025-06-16 14:22:14 +00:00
.HomeMap {
2025-03-31 15:26:29 +00:00
width: 100%;
height: 100%;
2025-06-16 14:22:14 +00:00
position: relative;
2025-04-01 16:12:21 +00:00
.zoom-level {
position: absolute;
bottom: 0.8%;
left: 1%;
2025-06-16 14:22:14 +00:00
z-index: 1000;
2025-04-01 16:12:21 +00:00
color: #fff;
2025-06-16 14:22:14 +00:00
background: rgba(0, 0, 0, 0.5);
padding: 5px;
border-radius: 3px;
2025-04-01 16:12:21 +00:00
}
2025-03-31 15:26:29 +00:00
2025-06-16 14:22:14 +00:00
.map-container {
2025-03-31 15:26:29 +00:00
width: 100%;
height: 100%;
}
2025-06-16 14:22:14 +00:00
2025-04-13 08:23:44 +00:00
.pointingNorth {
position: absolute;
bottom: 5%;
right: 25%;
cursor: pointer;
2025-06-16 14:22:14 +00:00
z-index: 1000;
2025-04-24 12:55:18 +00:00
img {
width: 35px;
height: 35px;
}
2025-04-13 08:23:44 +00:00
}
2025-03-31 15:26:29 +00:00
}
</style>