TaskSystem-admin/src/components/map/MapContainer.vue

148 lines
3.9 KiB
Vue
Raw Normal View History

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;
let autocomplete: any = null;
2023-08-17 10:01:53 +08:00
2023-08-17 18:26:00 +08:00
const emits = defineEmits("changeMaps");
const resetMap = () => {
markerList = [];
map.value.clearMap();
m_index = 0;
emits("changeMaps", markerList);
};
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 12:04:14 +08:00
AMap.plugin("AMap.Geocoder", function () {
geocoder = new AMap.Geocoder({
radius: 10, // 逆地理编码范围默认值1000单位
extensions: "all", // 返回地址描述结果时是否包含引擎内置的POI默认值base可选值base、all
}); // 经纬度数组
});
// // 创建 Autocomplete 实例
// autocomplete = new AMap.Autocomplete({
// city: "全国", // 设置默认城市为全国
// });
2023-08-17 12:08:04 +08:00
map.value.on("click", (e: any) => {
2023-08-17 18:26:00 +08:00
// console.log("点击", e.lnglat);
2023-08-17 16:17:58 +08:00
if (m_index >= 3) return;
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
// 自定义 Marker 的图标
var customIcon = new AMap.Icon({
size: new AMap.Size(36, 36), // 图标尺寸
image: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png", // 图标图片
imageSize: new AMap.Size(26, 26), // 图标图片尺寸
imageOffset: new AMap.Pixel(0, 0), // 图标图片偏移量
label: {
content: "起", // 文字内容
offset: new AMap.Pixel(-100, -100), // 文字相对于图标的偏移量
},
});
2023-08-17 12:08:04 +08:00
// 创建标记并添加到地图
2023-08-17 16:17:58 +08:00
let marker = new AMap.Marker({
2023-08-17 12:08:04 +08:00
position: e.lnglat,
offset: new AMap.Pixel(-10, -10),
title: "位置",
2023-08-17 16:17:58 +08:00
icon: customIcon,
// content: "起",
2023-08-17 12:08:04 +08:00
});
map.value.add(marker);
2023-08-17 16:17:58 +08:00
m_index++;
2023-08-17 12:08:04 +08:00
// 添加标记点击事件监听器
2023-08-17 18:26:00 +08:00
// marker.on("click", (event: any) => {
// console.log("删除", event);
// markerList = markerList.filter((item: any) => {
// item[0] == marker._position.pos[0] &&
// item[0] == marker._position.pos[0];
// });
// 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) => {
console.log(address);
ElMessage.error("搜索功能开发中");
// autocomplete.search(address, function (status: any, result: any) {
// console.log(status, result);
// if (status === "complete" && result.info === "OK") {
// // 获取提示结果
// var tips = result.tips;
// // 更新搜索结果列表或下拉菜单等
// // ...
// }
// });
};
// 获取地区名称
const getDistrictName = (lng: any, lat: any, e: any) => {
geocoder.getAddress([lng, lat], function (status: any, result: any) {
if (status === "complete" && result.info === "OK") {
// 解析成功,提取地区名称
var district = result.regeocode.addressComponent.district;
// console.log(district, result.regeocode); // 输出地区名字
markerList.push({
lnglat: e.lnglat,
address: result.regeocode.formattedAddress,
});
emits("changeMaps", markerList);
} else {
// 解析失败
console.log("逆地理编码失败");
}
});
};
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>