效果如图:
能搜索选择地址,也能点击地图选择地址。
代码如下:
首先下载loader。npm i @amap/amap-jsapi-loader --save
<template>
<div>
<el-dialog
title="选择地址"
:visible.sync="isMap"
:close-on-press-escape="false"
:close-on-click-modal="false"
:show-close="false"
width="700"
>
<div>
<div class="sear-map">
<el-row :gutter="20">
<el-col :span="20">
<!-- <el-input v-model="address" placeholder="输入详细地址"></el-input> -->
<el-select
style="width: 100%"
v-model="address"
filterable
remote
reserve-keyword
clearable
placeholder="搜地点"
:remote-method="remoteMethod"
:loading="loading"
@change="tipChange"
>
<el-option
v-for="item in tips"
:key="item.id"
:label="item.name"
:value="item.id"
>
<span style="float: left">{{ item.name }}</span>
<span style="float: right; color: #8492a6; font-size: 13px">{{
item.district
}}</span>
</el-option>
</el-select>
</el-col>
<el-col :span="4">
<el-button type="primary">搜索</el-button>
</el-col>
</el-row>
<el-row :gutter="20">
<el-col :span="8">
<el-input
style="margin-top: 20px"
disabled
v-model="lonLat"
placeholder="经纬度"
></el-input>
</el-col>
</el-row>
</div>
<div ref="asd" style="width: 100%; height: 600px"></div>
</div>
<div slot="footer">
<el-button @click="cancle">取 消</el-button>
<el-button type="primary" @click="mapSure">确 定</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import AMapLoader from "@amap/amap-jsapi-loader";
export default {
props: {
isMap: {
type: Boolean,
default: true,
},
},
data() {
return {
address: "", //详细地址
lonLat: "", //经纬度
loading: false,
tips: [],
projectMapMarker: undefined,
map: undefined,
};
},
created() {
},
mounted() {
let that = this;
AMapLoader.load({
key: "6dabd75cbdff12029510b4f886c90c46", // 申请好的Web端开发者Key,首次调用 load 时必填
version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
plugins: [
"AMap.DistrictSearch",
"AMap.Geocoder",
"AMap.AutoComplete ", //2.0版本是AMap.AutoComplete, 2.0以下是AMap.Autocomplete
// 输入提示插件
"AMap.PlaceSearch", // POI搜索插件
"AMap.Scale", // 右下角缩略图插件 比例尺
"AMap.OverView", // 地图鹰眼插件
"AMap.ToolBar", // 地图工具条
"AMap.MapType", // 类别切换控件,实现默认图层与卫星图、实施交通图层之间切换的控制
"AMap.PolyEditor", // 编辑 折线多,边形
"AMap.CircleEditor", // 圆形编辑器插件
"AMap.Geolocation", // 定位控件,用来获取和展示用户主机所在的经纬度位置], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
],
}).then((AMap) => {
that.initMap();
});
},
methods: {
cancle() {
this.$emit("closemap");
},
mapSure() {
if (this.address === "" || this.lonLat === "") {
this.$message({
message: "没有选择地址",
type: "warning",
});
} else {
let obj = {
address: this.address,
lonLat: this.lonLat,
};
this.$emit("getAddress", obj);
this.$emit("closemap");
}
},
//地图初始化
initMap() {
var chartDom = this.$refs.asd;
this.map = new AMap.Map(chartDom, {
resizeEnable: true,
zoom: 13, //地图显示的缩放级别
keyboardEnable: false,
center: [106.56288, 29.556742],
});
this.map.on("click", this.mapClick);
},
//远程搜所
remoteMethod(query) {
if (query !== "") {
this.loading = true;
const that = this;
AMap.plugin("AMap.AutoComplete", function () {
// 实例化Autocomplete
const autoOptions = {
city: "重庆",
};
const autoComplete = new AMap.AutoComplete(autoOptions);
autoComplete.search(query, function (status, result) {
let res = result.tips || []; // 搜索成功时,result即是对应的匹配数据
that.tips = res.filter((item) => {
return item.id !== "";
});
that.loading = false;
});
});
} else {
this.tips = [];
}
},
tipChange(value) {
//下拉选择
for (const tip of this.tips) {
if (value === tip.id) {
if (this.projectMapMarker !== undefined) {
// 如果点标记已存在则先移除原点
this.map.remove(this.projectMapMarker);
this.lonLat = "";
}
this.projectMapMarker = new AMap.Marker({
position: new AMap.LngLat(tip.location.lng, tip.location.lat),
});
this.lonLat = tip.location.lng + "," + tip.location.lat;
this.map.add(this.projectMapMarker); // 添加点标记在地图上
this.map.setZoom(13);
this.map.setCenter(tip.location);
this.regeoCode();
break;
}
}
},
mapClick(e) {
// 地图点击事件
if (this.map !== undefined) {
if (this.projectMapMarker !== undefined) {
// 如果点标记已存在则先移除原点
this.map.remove(this.projectMapMarker);
this.lonLat = "";
}
this.projectMapMarker = new AMap.Marker({
// 定义点标记对象
position: new AMap.LngLat(e.lnglat.lng, e.lnglat.lat),
});
this.lonLat = e.lnglat.lng + "," + e.lnglat.lat;
this.map.add(this.projectMapMarker); // 添加点标记在地图上
this.regeoCode(); //把经纬度转化为地址
}
},
regeoCode() {
// 把拿到的经纬度转化为地址信息
let geocoder = new AMap.Geocoder({
city: "010", //城市设为北京,默认:“全国”
radius: 1000, //范围,默认:500
});
const that = this; // 注意this指向问题
let lnglat = this.lonLat.split(",");
this.map.add(this.projectMapMarker);
this.projectMapMarker.setPosition(lnglat);
geocoder.getAddress(lnglat, function (status, result) {
if (status === "complete" && result.regeocode) {
// let street = result.regeocode.addressComponent.street;
// let streetNumber = result.regeocode.addressComponent.streetNumber;
// let township = result.regeocode.addressComponent.township;
// console.log(street+streetNumber+township);
// let sindex = result.regeocode.formattedAddress.indexOf('县');
// that.address=result.regeocode.formattedAddress.substring(sindex+1)
that.address = result.regeocode.formattedAddress;
} else {
log.error("根据经纬度查询地址失败");
}
});
},
},
};
</script>
<style scoped>
.sear-map {
margin-bottom: 20px;
}
</style>
版权声明:本文为wyh_115原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。