2021-05-13 10:56:04 +08:00
|
|
|
|
<template>
|
2023-08-03 16:36:41 +08:00
|
|
|
|
<div class="map">
|
|
|
|
|
|
|
|
|
|
<div class="address">{{ addrContent.address }}</div>
|
|
|
|
|
<div id="map-container"></div>
|
|
|
|
|
|
|
|
|
|
<div class="search-con">
|
|
|
|
|
<Input placeholder="输入关键字搜索" id="input-map" v-model="mapSearch" />
|
|
|
|
|
<ul>
|
|
|
|
|
<li v-for="(tip, index) in tips" :key="index" @click="selectAddr(tip.location)">
|
|
|
|
|
<p>{{ tip.name }}</p>
|
|
|
|
|
<p>{{ tip.district + tip.address }}</p>
|
|
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
<div slot="footer" class="footer">
|
|
|
|
|
|
|
|
|
|
<Button type="primary" :loading="loading" @click="ok">确定</Button>
|
|
|
|
|
</div>
|
|
|
|
|
|
2021-05-13 10:56:04 +08:00
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
<script>
|
2023-08-03 16:36:41 +08:00
|
|
|
|
import AMapLoader from "@amap/amap-jsapi-loader";
|
|
|
|
|
import { handleRegion } from "@/api/address.js";
|
2021-11-18 17:10:44 +08:00
|
|
|
|
|
2023-08-03 16:36:41 +08:00
|
|
|
|
const config = require('@/config/index')
|
2021-05-13 10:56:04 +08:00
|
|
|
|
export default {
|
2023-08-03 16:36:41 +08:00
|
|
|
|
name: "map",
|
|
|
|
|
data() {
|
2021-05-13 10:56:04 +08:00
|
|
|
|
return {
|
2023-08-03 16:36:41 +08:00
|
|
|
|
config,
|
|
|
|
|
showMap: false, // 地图显隐
|
|
|
|
|
mapSearch: "", // 地图搜索
|
2021-05-13 10:56:04 +08:00
|
|
|
|
map: null, // 初始化地图
|
|
|
|
|
autoComplete: null, // 初始化搜索方法
|
|
|
|
|
geocoder: null, // 初始化地理、坐标转化
|
|
|
|
|
positionPicker: null, // 地图拖拽选点
|
2023-08-03 16:36:41 +08:00
|
|
|
|
tips: [], //搜索关键字列表
|
2021-05-13 10:56:04 +08:00
|
|
|
|
addrContent: {}, // 回显地址信息
|
2023-08-03 16:36:41 +08:00
|
|
|
|
loading: false, // 加载状态
|
2021-05-13 10:56:04 +08:00
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
watch: {
|
|
|
|
|
mapSearch: function (val) {
|
|
|
|
|
this.searchOfMap(val);
|
2023-08-03 16:36:41 +08:00
|
|
|
|
},
|
2021-05-13 10:56:04 +08:00
|
|
|
|
},
|
|
|
|
|
methods: {
|
2023-08-03 16:36:41 +08:00
|
|
|
|
ok() {
|
|
|
|
|
|
|
|
|
|
if (this.addrContent && this.addrContent.regeocode) {
|
|
|
|
|
const params = {
|
|
|
|
|
cityCode: this.addrContent.regeocode.addressComponent.citycode,
|
|
|
|
|
townName: this.addrContent.regeocode.addressComponent.township,
|
|
|
|
|
};
|
2021-05-13 10:56:04 +08:00
|
|
|
|
handleRegion(params).then((res) => {
|
2021-05-17 16:04:36 +08:00
|
|
|
|
if (res.success) {
|
2023-08-03 16:36:41 +08:00
|
|
|
|
this.addrContent.addr = res.result.name.replace(/,/g, " ");
|
2021-05-13 10:56:04 +08:00
|
|
|
|
this.addrContent.addrId = res.result.id;
|
2023-08-03 16:36:41 +08:00
|
|
|
|
this.loading = false;
|
|
|
|
|
|
|
|
|
|
this.$emit("getAddress", this.addrContent);
|
2021-05-13 10:56:04 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2023-08-03 16:36:41 +08:00
|
|
|
|
this.$Message.error('未获取到坐标信息!请查看高德API配置是否正确')
|
2021-05-13 10:56:04 +08:00
|
|
|
|
}
|
2023-08-03 16:36:41 +08:00
|
|
|
|
|
2021-05-13 10:56:04 +08:00
|
|
|
|
},
|
2023-08-03 16:36:41 +08:00
|
|
|
|
init() {
|
2021-05-13 10:56:04 +08:00
|
|
|
|
AMapLoader.load({
|
2021-09-22 15:08:34 +08:00
|
|
|
|
key: this.config.aMapKey, // 申请好的Web端开发者Key,首次调用 load 时必填
|
2023-08-03 16:36:41 +08:00
|
|
|
|
version: "", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
|
2021-05-13 10:56:04 +08:00
|
|
|
|
plugins: [
|
2023-08-03 16:36:41 +08:00
|
|
|
|
"AMap.ToolBar",
|
|
|
|
|
"AMap.Autocomplete",
|
|
|
|
|
"AMap.PlaceSearch",
|
|
|
|
|
"AMap.Geolocation",
|
|
|
|
|
"AMap.Geocoder",
|
2021-05-13 10:56:04 +08:00
|
|
|
|
], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
|
|
|
|
|
AMapUI: {
|
|
|
|
|
// 是否加载 AMapUI,缺省不加载
|
2023-08-03 16:36:41 +08:00
|
|
|
|
version: "1.1", // AMapUI 缺省 1.1
|
|
|
|
|
plugins: ["misc/PositionPicker"], // 需要加载的 AMapUI ui插件
|
|
|
|
|
},
|
2021-05-13 10:56:04 +08:00
|
|
|
|
})
|
|
|
|
|
.then((AMap) => {
|
|
|
|
|
let that = this;
|
2023-08-03 16:36:41 +08:00
|
|
|
|
this.map = new AMap.Map("map-container", {
|
|
|
|
|
zoom: 12,
|
2021-05-13 10:56:04 +08:00
|
|
|
|
});
|
|
|
|
|
that.map.addControl(new AMap.ToolBar());
|
|
|
|
|
that.map.addControl(new AMap.Autocomplete());
|
|
|
|
|
that.map.addControl(new AMap.PlaceSearch());
|
|
|
|
|
that.map.addControl(new AMap.Geocoder());
|
|
|
|
|
|
|
|
|
|
// 实例化Autocomplete
|
|
|
|
|
let autoOptions = {
|
2023-08-03 16:36:41 +08:00
|
|
|
|
city: "全国",
|
2021-05-13 10:56:04 +08:00
|
|
|
|
};
|
|
|
|
|
that.autoComplete = new AMap.Autocomplete(autoOptions); // 搜索
|
|
|
|
|
that.geocoder = new AMap.Geocoder(autoOptions);
|
|
|
|
|
|
|
|
|
|
that.positionPicker = new AMapUI.PositionPicker({
|
|
|
|
|
// 拖拽选点
|
2023-08-03 16:36:41 +08:00
|
|
|
|
mode: "dragMap",
|
|
|
|
|
map: that.map,
|
2021-05-13 10:56:04 +08:00
|
|
|
|
});
|
|
|
|
|
that.positionPicker.start();
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* 所有回显数据,都在positionResult里面
|
|
|
|
|
* 需要字段可以查找
|
|
|
|
|
*
|
|
|
|
|
*/
|
2023-08-03 16:36:41 +08:00
|
|
|
|
that.positionPicker.on("success", function (positionResult) {
|
2021-05-13 10:56:04 +08:00
|
|
|
|
that.addrContent = positionResult;
|
|
|
|
|
});
|
|
|
|
|
})
|
2023-08-03 16:36:41 +08:00
|
|
|
|
.catch((e) => { });
|
2021-05-13 10:56:04 +08:00
|
|
|
|
},
|
2023-08-03 16:36:41 +08:00
|
|
|
|
searchOfMap(val) {
|
2021-05-13 10:56:04 +08:00
|
|
|
|
// 地图搜索
|
|
|
|
|
let that = this;
|
|
|
|
|
this.autoComplete.search(val, function (status, result) {
|
|
|
|
|
// 搜索成功时,result即是对应的匹配数据
|
2023-08-03 16:36:41 +08:00
|
|
|
|
if (status == "complete" && result.info == "OK") {
|
2021-05-13 10:56:04 +08:00
|
|
|
|
that.tips = result.tips;
|
|
|
|
|
} else {
|
|
|
|
|
that.tips = [];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
2023-08-03 16:36:41 +08:00
|
|
|
|
selectAddr(location) {
|
2021-05-13 10:56:04 +08:00
|
|
|
|
// 选择坐标
|
|
|
|
|
if (!location) {
|
2023-08-03 16:36:41 +08:00
|
|
|
|
this.$Message.warning("请选择正确点位");
|
2021-05-13 10:56:04 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
const lnglat = [location.lng, location.lat];
|
|
|
|
|
this.positionPicker.start(lnglat);
|
2023-08-03 16:36:41 +08:00
|
|
|
|
},
|
2021-05-13 10:56:04 +08:00
|
|
|
|
},
|
2023-08-03 16:36:41 +08:00
|
|
|
|
mounted() {
|
2021-05-13 10:56:04 +08:00
|
|
|
|
this.init();
|
2023-08-03 16:36:41 +08:00
|
|
|
|
},
|
2021-05-13 10:56:04 +08:00
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
|
#map-container {
|
|
|
|
|
width: 500px;
|
|
|
|
|
height: 400px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.search-con {
|
|
|
|
|
position: absolute;
|
|
|
|
|
right: 20px;
|
|
|
|
|
top: 64px;
|
|
|
|
|
width: 260px;
|
2023-08-03 16:36:41 +08:00
|
|
|
|
|
2021-05-13 10:56:04 +08:00
|
|
|
|
ul {
|
|
|
|
|
width: 260px;
|
2023-08-03 16:36:41 +08:00
|
|
|
|
height: 360px;
|
2021-05-13 10:56:04 +08:00
|
|
|
|
overflow: scroll;
|
2023-08-03 16:36:41 +08:00
|
|
|
|
|
2021-05-13 10:56:04 +08:00
|
|
|
|
li {
|
|
|
|
|
padding: 5px;
|
2023-08-03 16:36:41 +08:00
|
|
|
|
|
2021-05-13 10:56:04 +08:00
|
|
|
|
p:nth-child(2) {
|
|
|
|
|
color: #999;
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
}
|
2023-08-03 16:36:41 +08:00
|
|
|
|
|
2021-05-13 10:56:04 +08:00
|
|
|
|
&:hover {
|
|
|
|
|
background-color: #eee;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.address {
|
|
|
|
|
margin-bottom: 10px;
|
2023-08-03 16:36:41 +08:00
|
|
|
|
// color: $theme_color;
|
2021-05-13 10:56:04 +08:00
|
|
|
|
font-weight: bold;
|
|
|
|
|
}
|
2023-08-03 16:36:41 +08:00
|
|
|
|
|
|
|
|
|
.footer {
|
|
|
|
|
text-align: right;
|
|
|
|
|
margin: 10px 0;
|
|
|
|
|
}
|
2021-05-13 10:56:04 +08:00
|
|
|
|
</style>
|