2023-08-17 10:01:53 +08:00
|
|
|
|
<template>
|
2023-08-17 11:23:12 +08:00
|
|
|
|
<div id="container" ref="map"></div>
|
2023-08-17 10:01:53 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script lang="ts" setup name="mapContainer">
|
|
|
|
|
import AMapLoader from "@amap/amap-jsapi-loader";
|
|
|
|
|
import { shallowRef } from "@vue/reactivity";
|
|
|
|
|
|
|
|
|
|
const map = shallowRef(null);
|
2023-08-17 12:08:04 +08:00
|
|
|
|
let AMap: any = null;
|
2023-08-17 16:17:58 +08:00
|
|
|
|
let markerList: Array<any> = [];
|
|
|
|
|
let m_index: Number = 0;
|
2023-08-18 12:04:14 +08:00
|
|
|
|
let geocoder: any = null;
|
2023-08-22 14:16:51 +08:00
|
|
|
|
let marker: any = null;
|
2023-08-17 10:01:53 +08:00
|
|
|
|
|
2023-08-18 15:35:32 +08:00
|
|
|
|
const emits = defineEmits(["changeMaps"]);
|
2023-08-17 18:26:00 +08:00
|
|
|
|
|
|
|
|
|
const resetMap = () => {
|
2023-08-18 15:35:32 +08:00
|
|
|
|
try {
|
|
|
|
|
markerList = [];
|
|
|
|
|
map.value?.clearMap();
|
|
|
|
|
m_index = 0;
|
|
|
|
|
emits("changeMaps", markerList);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
}
|
2023-08-17 18:26:00 +08:00
|
|
|
|
};
|
|
|
|
|
|
2023-08-17 12:08:04 +08:00
|
|
|
|
const initMap = async () => {
|
|
|
|
|
const loader = AMapLoader.load({
|
|
|
|
|
key: "4f8f55618010007147aab96fc72bb408",
|
|
|
|
|
version: "2.0",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
AMap = await loader;
|
|
|
|
|
|
|
|
|
|
map.value = new AMap.Map("container", {
|
|
|
|
|
zoom: 13,
|
2023-08-18 15:35:32 +08:00
|
|
|
|
viewMode: "2D",
|
2023-08-22 14:16:51 +08:00
|
|
|
|
center: [105.4423, 28.8717],
|
2023-08-17 12:08:04 +08:00
|
|
|
|
});
|
|
|
|
|
|
2023-08-18 12:04:14 +08:00
|
|
|
|
AMap.plugin("AMap.Geocoder", function () {
|
|
|
|
|
geocoder = new AMap.Geocoder({
|
|
|
|
|
radius: 10, // 逆地理编码范围,默认值:1000,单位:米
|
|
|
|
|
extensions: "all", // 返回地址描述结果时是否包含引擎内置的POI,默认值:base,可选值:base、all
|
|
|
|
|
}); // 经纬度数组
|
|
|
|
|
});
|
|
|
|
|
|
2023-08-22 14:16:51 +08:00
|
|
|
|
AMap.plugin("AMap.Geolocation", function () {
|
|
|
|
|
let geolocation = new AMap.Geolocation({
|
|
|
|
|
enableHighAccuracy: true, // 是否使用高精度定位,默认:true
|
|
|
|
|
timeout: 10000, // 超过1秒后停止定位,默认:无穷大
|
|
|
|
|
buttonPosition: "RB", // 定位按钮的停靠位置
|
|
|
|
|
zoomToAccuracy: true, // 定位成功后是否自动调整地图视野到定位点
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
map.value.addControl(geolocation);
|
|
|
|
|
|
|
|
|
|
// geolocation.getCurrentPosition(function (status, result) {
|
|
|
|
|
// console.log(status, result);
|
|
|
|
|
// });
|
|
|
|
|
});
|
|
|
|
|
|
2023-08-18 15:35:32 +08:00
|
|
|
|
map.value.setFitView();
|
2023-08-18 12:04:14 +08:00
|
|
|
|
|
2023-08-17 12:08:04 +08:00
|
|
|
|
map.value.on("click", (e: any) => {
|
2023-08-18 15:35:32 +08:00
|
|
|
|
// if (m_index >= 1) return;
|
|
|
|
|
if (m_index >= 1) {
|
|
|
|
|
map.value?.remove(marker);
|
|
|
|
|
m_index = 0;
|
|
|
|
|
}
|
2023-08-17 18:26:00 +08:00
|
|
|
|
// 调用函数并传入经纬度
|
2023-08-18 12:04:14 +08:00
|
|
|
|
getDistrictName(e.lnglat.lng, e.lnglat.lat, e);
|
2023-08-17 16:17:58 +08:00
|
|
|
|
|
2023-08-17 12:08:04 +08:00
|
|
|
|
// 创建标记并添加到地图
|
2023-08-18 15:35:32 +08:00
|
|
|
|
marker = new AMap.Marker({
|
2023-08-17 12:08:04 +08:00
|
|
|
|
position: e.lnglat,
|
2023-08-18 15:35:32 +08:00
|
|
|
|
offset: new AMap.Pixel(0, 0),
|
2023-08-17 12:08:04 +08:00
|
|
|
|
title: "位置",
|
|
|
|
|
});
|
|
|
|
|
map.value.add(marker);
|
2023-08-17 16:17:58 +08:00
|
|
|
|
m_index++;
|
2023-08-17 12:08:04 +08:00
|
|
|
|
|
|
|
|
|
// 添加标记点击事件监听器
|
2023-08-18 12:10:37 +08:00
|
|
|
|
marker.on("click", (event: any) => {
|
2023-08-18 15:35:32 +08:00
|
|
|
|
// markerList = markerList.filter((item: any) => {
|
|
|
|
|
// item[0] == marker._position.pos[0] &&
|
|
|
|
|
// item[0] == marker._position.pos[0];
|
|
|
|
|
// });
|
|
|
|
|
markerList = [];
|
2023-08-18 12:10:37 +08:00
|
|
|
|
emits("changeMaps", markerList);
|
|
|
|
|
map.value.remove(marker);
|
|
|
|
|
m_index--;
|
|
|
|
|
marker = null;
|
|
|
|
|
});
|
2023-08-17 12:08:04 +08:00
|
|
|
|
});
|
2023-08-17 10:01:53 +08:00
|
|
|
|
};
|
2023-08-17 11:23:12 +08:00
|
|
|
|
|
2023-08-18 12:04:14 +08:00
|
|
|
|
const searchMap = (address: any) => {
|
2023-08-18 15:35:32 +08:00
|
|
|
|
// ElMessage.error("搜索功能开发中");
|
|
|
|
|
AMap.plugin("AMap.PlaceSearch", async function () {
|
|
|
|
|
try {
|
|
|
|
|
const placeSearch = new AMap.PlaceSearch({
|
|
|
|
|
pageSize: 5, // 单页显示结果条数
|
|
|
|
|
pageIndex: 1, // 页码
|
|
|
|
|
city: "510500", // 兴趣点城市
|
|
|
|
|
citylimit: true, //是否强制限制在设置的城市内搜索
|
|
|
|
|
map: map.value, // 展现结果的地图实例
|
|
|
|
|
panel: false, // 结果列表将在此容器中进行展示。
|
|
|
|
|
autoFitView: true, // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
|
|
|
|
|
});
|
|
|
|
|
AMap.Event.addListener(placeSearch, "markerClick", (e: any) => {
|
|
|
|
|
marker ? map.value?.remove(marker) : null;
|
|
|
|
|
getDistrictName(e.event.lnglat.lng, e.event.lnglat.lat, e.event);
|
|
|
|
|
});
|
|
|
|
|
placeSearch.search(address);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
ElMessage.error("没找到");
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-08-18 12:04:14 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 获取地区名称
|
|
|
|
|
const getDistrictName = (lng: any, lat: any, e: any) => {
|
|
|
|
|
geocoder.getAddress([lng, lat], function (status: any, result: any) {
|
|
|
|
|
if (status === "complete" && result.info === "OK") {
|
2023-08-18 15:35:32 +08:00
|
|
|
|
markerList = [];
|
2023-08-18 12:04:14 +08:00
|
|
|
|
markerList.push({
|
|
|
|
|
lnglat: e.lnglat,
|
|
|
|
|
address: result.regeocode.formattedAddress,
|
|
|
|
|
});
|
|
|
|
|
emits("changeMaps", markerList);
|
|
|
|
|
} else {
|
|
|
|
|
// 解析失败
|
2023-08-18 15:35:32 +08:00
|
|
|
|
ElMessage.error("逆地理编码失败");
|
2023-08-18 12:04:14 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
|
resetMap,
|
|
|
|
|
searchMap,
|
|
|
|
|
});
|
|
|
|
|
|
2023-08-17 11:23:12 +08:00
|
|
|
|
onMounted(() => {
|
|
|
|
|
initMap();
|
|
|
|
|
});
|
2023-08-17 10:01:53 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
|
#container {
|
|
|
|
|
padding: 0px;
|
|
|
|
|
margin: 0px;
|
2023-08-17 11:23:12 +08:00
|
|
|
|
width: 400px;
|
|
|
|
|
height: 400px;
|
2023-08-17 10:01:53 +08:00
|
|
|
|
}
|
|
|
|
|
</style>
|