对于一些项目需要在组件中插入地图,可以选取位置以及经纬度
可以把下面的代码写入到子组件作为弹出的地图demo
<template>
<div class="amap-ref">
<div class="amap-cointainer" v-loading="mapLoading">
<el-row class="amap-row" :gutter="16">
<el-col :span="19">
<el-amap-search-box class="amap-ref-search-box" ref="searchBox" :on-search-result="onSearchResult">el-amap-search-box>
el-col>
<el-col :span="1" :offset="2" style="padding-top:5px">
<el-button type="info" @click="confirmClick">确定el-button>
el-col>
el-row>
<el-amap :plugin="shopAmap.plugin" :center="mapCenter" :events="amapEvents()" :zoom="12">
<el-amap-marker v-for="marker in shopAmap.markers" key="marker" :position="marker" :events="markerEvent()" :draggable="true">el-amap-marker>
el-amap>
<el-input v-model="shopAmap.currentMarker.formattedAddress" disabled style="margin-top:1em">el-input>
div>
div>
template>
<script>
import { lazyAMapApiLoaderInstance } from 'vue-amap';
export default {
created() {
lazyAMapApiLoaderInstance.load().then(() => {
this.geocoder = new AMap.Geocoder({});
this.GeolocationResult = new AMap.Geolocation();
});
},
watch:{
value(val){
this.currentValue = val;
},
x(val){
this.shopAmap.lng = val;
},
y(val){
this.shopAmap.lat = val;
},
},
data() {
return{
mapLoading:false,
name:'',
currentValue:this.value,
shopAmap:{
lat:0,
lng:0,
mapCenter:[116.412267, 39.902825],
markers:[],
currentMarker:{
formattedAddress:this.value
},
plugin: ['ToolBar','Scale','Geolocation'],
},
};
},
computed: {
mapCenter: function () {
return [this.shopAmap.lng+'',this.shopAmap.lat+''];
}
},
methods:{
getOldMap(val){
this.name=val.name
this.$refs.searchBox.search()
this.x=val.latitude
this.y=val.longitude
this.value=val.address
this.shopAmap.markers=[]
this.$nextTick(()=>{
document.getElementsByClassName('search-box-wrapper')[0].getElementsByTagName('input')[0].value=""
if(val.latitude && val.longitude){
this.shopAmap.lat = val.longitude;
this.shopAmap.lng = val.latitude;
this.shopAmap.currentMarker.formattedAddress=val.address
this.shopAmap.markers.push([val.latitude,val.longitude]);
}else{
this.setMap(val.address)
}
})
},
setMap(str){
var that = this;
var placeSearch= new AMap.PlaceSearch();
placeSearch.search(str, function(status, result) {
var pois = [];
for(var i=0;i<result.poiList.pois.length;i++){
pois.push(result.poiList.pois[i].location);
}
that.onSearchResult(pois);
});
},
confirmClick:function(){
let mapObj={
name:this.name,
formattedAddress:this.shopAmap.currentMarker.formattedAddress,
latitude:this.shopAmap.markers[0][0],
longitude:this.shopAmap.markers[0][1],
}
this.currentValue = this.shopAmap.currentMarker.formattedAddress;
this.$emit('input',mapObj);
},
generalAddres:function(x,y){
var that = this;
that.shopAmap.markers=[[x,y]];
var lnglatXY=[x,y];
that.mapLoading=true
that.geocoder.getAddress(lnglatXY, function(status, result) {
if (status === 'complete' && result.info === 'OK') {
that.mapLoading=false
that.shopAmap.currentMarker = result.regeocode;
}else{
that.mapLoading=false
console.error('根据坐标获取详细地址失败')
}
});
},
onSearchResult(pois) {
let latSum = 0;
let lngSum = 0;
if (pois.length > 0) {
pois.forEach(poi => {
let {lng, lat} = poi;
lngSum += lng;
latSum += lat;
});
let center = {
lng: lngSum / pois.length,
lat: latSum / pois.length
};
this.shopAmap.lng = center.lng;
this.shopAmap.lat = center.lat;
this.shopAmap.markers = [[center.lng, center.lat]];
this.generalAddres(center.lng, center.lat);
}
},
amapEvents(){
var that = this;
return{
click:(current)=>{
that.generalAddres(current.lnglat.lng,current.lnglat.lat);
}
}
},
markerEvent(){
var that = this;
return {
click:(a,b,c,d)=>{
},
dragend:(current)=>{
that.generalAddres(current.lnglat.lng,current.lnglat.lat);
}
}
},
}
}
script>
有关地图的参考网址以及地图api
https://elemefe.github.io/vue-amap/#/zh-cn/base/amap