更新地图选择

This commit is contained in:
weipengfei 2023-08-18 15:35:32 +08:00
parent 6f4cbcc24a
commit 9894b63c47
3 changed files with 54 additions and 57 deletions

View File

@ -11,15 +11,19 @@ let AMap: any = null;
let markerList: Array<any> = []; let markerList: Array<any> = [];
let m_index: Number = 0; let m_index: Number = 0;
let geocoder: any = null; let geocoder: any = null;
let autocomplete: any = null; let marker = null;
const emits = defineEmits("changeMaps"); const emits = defineEmits(["changeMaps"]);
const resetMap = () => { const resetMap = () => {
try {
markerList = []; markerList = [];
map.value.clearMap(); map.value?.clearMap();
m_index = 0; m_index = 0;
emits("changeMaps", markerList); emits("changeMaps", markerList);
} catch (error) {
console.log(error);
}
}; };
const initMap = async () => { const initMap = async () => {
@ -32,6 +36,7 @@ const initMap = async () => {
map.value = new AMap.Map("container", { map.value = new AMap.Map("container", {
zoom: 13, zoom: 13,
viewMode: "2D",
}); });
AMap.plugin("AMap.Geocoder", function () { AMap.plugin("AMap.Geocoder", function () {
@ -41,49 +46,34 @@ const initMap = async () => {
}); // }); //
}); });
// // Autocomplete map.value.setFitView();
// autocomplete = new AMap.Autocomplete({
// city: "", //
// });
map.value.on("click", (e: any) => { map.value.on("click", (e: any) => {
// console.log("", e.lnglat); // if (m_index >= 1) return;
if (m_index >= 1) return; if (m_index >= 1) {
map.value?.remove(marker);
m_index = 0;
}
// //
getDistrictName(e.lnglat.lng, e.lnglat.lat, e); getDistrictName(e.lnglat.lng, e.lnglat.lat, e);
// 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), //
},
});
// //
let marker = new AMap.Marker({ marker = new AMap.Marker({
position: e.lnglat, position: e.lnglat,
offset: new AMap.Pixel(-10, -10), offset: new AMap.Pixel(0, 0),
title: "位置", title: "位置",
icon: customIcon,
// content: "",
}); });
map.value.add(marker); map.value.add(marker);
m_index++; m_index++;
// //
marker.on("click", (event: any) => { marker.on("click", (event: any) => {
console.log("删除", event); // markerList = markerList.filter((item: any) => {
markerList = markerList.filter((item: any) => { // item[0] == marker._position.pos[0] &&
item[0] == marker._position.pos[0] && // item[0] == marker._position.pos[0];
item[0] == marker._position.pos[0]; // });
}); markerList = [];
emits("changeMaps", markerList); emits("changeMaps", markerList);
//
map.value.remove(marker); map.value.remove(marker);
m_index--; m_index--;
marker = null; marker = null;
@ -92,29 +82,34 @@ const initMap = async () => {
}; };
const searchMap = (address: any) => { const searchMap = (address: any) => {
console.log(address); // ElMessage.error("");
ElMessage.error("搜索功能开发中"); AMap.plugin("AMap.PlaceSearch", async function () {
try {
// autocomplete.search(address, function (status: any, result: any) { const placeSearch = new AMap.PlaceSearch({
// console.log(status, result); pageSize: 5, //
pageIndex: 1, //
// if (status === "complete" && result.info === "OK") { city: "510500", //
// // citylimit: true, //
// var tips = result.tips; 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("没找到");
}
});
}; };
// //
const getDistrictName = (lng: any, lat: any, e: any) => { const getDistrictName = (lng: any, lat: any, e: any) => {
geocoder.getAddress([lng, lat], function (status: any, result: any) { geocoder.getAddress([lng, lat], function (status: any, result: any) {
if (status === "complete" && result.info === "OK") { if (status === "complete" && result.info === "OK") {
// markerList = [];
var district = result.regeocode.addressComponent.district;
// console.log(district, result.regeocode); //
markerList.push({ markerList.push({
lnglat: e.lnglat, lnglat: e.lnglat,
address: result.regeocode.formattedAddress, address: result.regeocode.formattedAddress,
@ -122,7 +117,7 @@ const getDistrictName = (lng: any, lat: any, e: any) => {
emits("changeMaps", markerList); emits("changeMaps", markerList);
} else { } else {
// //
console.log("逆地理编码失败"); ElMessage.error("逆地理编码失败");
} }
}); });
}; };

View File

@ -111,7 +111,7 @@
@click="changeMap(0)" @click="changeMap(0)"
> >
<el-input <el-input
placeholder="请点击上方地图选择地点" placeholder="请选择起点"
readonly readonly
:value="formData.extend?.origin?.address" :value="formData.extend?.origin?.address"
/> />
@ -122,7 +122,7 @@
@click="changeMap(1)" @click="changeMap(1)"
> >
<el-input <el-input
placeholder="请点击上方地图选择地点" placeholder="请选择中转点"
readonly readonly
:value="formData.extend?.transfer?.address" :value="formData.extend?.transfer?.address"
/> />
@ -133,7 +133,7 @@
@click="changeMap(2)" @click="changeMap(2)"
> >
<el-input <el-input
placeholder="请点击上方地图选择地点" placeholder="请选择终点"
readonly readonly
:value="formData.extend?.terminus?.address" :value="formData.extend?.terminus?.address"
/> />

View File

@ -17,7 +17,7 @@
clearable clearable
/> />
<el-button type="primary" @click="serach()">搜索</el-button> <el-button type="primary" @click="serach()">搜索</el-button>
<!-- <el-button @click="resetMap()">重置</el-button> --> <el-button @click="resetMap()">重置</el-button>
</el-form-item> </el-form-item>
<el-form-item label-width="80px" label="地图"> <el-form-item label-width="80px" label="地图">
<MapContainer ref="mapRef" @changeMaps="changeMaps"></MapContainer> <MapContainer ref="mapRef" @changeMaps="changeMaps"></MapContainer>
@ -124,14 +124,16 @@ const changeMaps = (e: any) => {
// //
const handleSubmit = async () => { const handleSubmit = async () => {
// if (address.value.length < 3) return ElMessage.error(""); // if (address.value.length < 3) return ElMessage.error("");
if (address.value.length < 1) return ElMessage.error("请先选择地点");
popupRef.value?.close(); popupRef.value?.close();
emit("success", address.value); emit("success", address.value);
}; };
// //
const open = (type = "地点") => { const open = async (type = "地点") => {
title.value = "选择" + type; title.value = "选择" + type;
popupRef.value?.open(); popupRef.value?.open();
await nextTick();
resetMap(); resetMap();
}; };