vue-amap根據(jù)地址回顯地圖并mark的操作
實現(xiàn)了地圖選址功能后,現(xiàn)在來實現(xiàn)一個根據(jù)地址回顯地圖并標記的功能,效果圖如下:

直接上代碼:
main.js中引入
import Vue from 'vue'
import VueAMap from 'vue-amap'
import App from './App'
import router from './router'
Vue.config.productionTip = false
localStorage.clear();
Vue.use(VueAMap)
VueAMap.initAMapApiLoader({
//高德的key
key: "cc644a48b701c256e9a827f068743fdd",
// 插件集合
plugin: [
"AMap.Autocomplete",// 輸入提示插件
"AMap.PlaceSearch",// POI搜索插件
"AMap.Scale",// 右下角縮略圖插件 比例尺
"AMap.OverView",// 地圖鷹眼插件
"AMap.ToolBar",// 地圖工具條
"AMap.MapType",// 類別切換控件,實現(xiàn)默認圖層與衛(wèi)星圖、實施交通圖層之間切換的控制
"AMap.PolyEditor",// 編輯 折線多,邊形
"AMap.CircleEditor",// 圓形編輯器插件
"AMap.Geolocation", // 定位控件,用來獲取和展示用戶主機所在的經(jīng)緯度位置
"AMap.Geocoder",// 地理編碼與逆地理編碼服務,用于地址描述與坐標間的相互轉(zhuǎn)換
"AMap.AMapUI",// UI組件
],
v: "1.4.4",
uiVersion: "1.0.11" // 版本號
});
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
調(diào)用showMap組件的文件
<template>
<div class="myIndexWrap">
<!-- 根據(jù)地址信息顯示地圖位置 -->
<div class="addrMapWrapper">
<h3>根據(jù)地址信息顯示地圖位置</h3>
<div class="addrWrapper">
<p>{{selAddr}}</p>
<ShowMap class="showMapBox" :selAddr="selAddr"/>
</div>
</div>
</div>
</template>
<script type='text/ecmascript-6'>
import ShowMap from "@/components/ShowMap.vue";//地圖標記
export default {
components: {
ShowMap
},
data() {
return {
selAddr:'江蘇省南京市雨花臺區(qū)新華匯B2座',
};
},
};
</script>
<style lang='scss' scoped>
.myIndexWrap {
width: 1200px;
min-height: 800px;
height: auto;
border: 1px solid #000;
display: flex;
flex-flow: column;
h3 {
padding-left: 50px;
}
// 地圖標記
.addrMapWrapper {
.addrWrapper{
margin-left: 50px;
}
}
}
</style>
ShowMap.vue
<template> <div class="showMapWrapper"> <el-amap vid="map" class="amap-box" :zoom="zoom" :plugin="plugin" :events="events" :center="center" > <el-amap-marker vid="marker" :position="center" :label="label"></el-amap-marker> </el-amap> </div> </template>
<script type='text/ecmascript-6'>
export default {
components: {},
props:{
selAddr:{
type:String,
default:''
}
},
data() {
let self = this;
return {
zoom: 18,
lng: 0,
lat: 0,
loaded: false,
address: this.selAddr,
//mark的時候顯示tip
label:{
content:this.selAddr,
offset:[10,12]
},
center: [0,0],
//插件集合
plugin: [
//工具條
{
pName: "ToolBar",
positon: "LB"
},
],
events: {
init(map) {
// 這里通過高德 SDK 完成。
var geocoder = new AMap.Geocoder({
radius: 1000,
extensions: "all",
city: "全國"
});
geocoder.getLocation(self.address, (status, result) => {
if (status === "complete" && result.geocodes.length) {
let lnglat = result.geocodes[0].location
self.lng = lnglat.lng;
self.lat = lnglat.lat;
self.center = [self.lng, self.lat];
}
});
}
}
};
},
};
</script>
<style lang='scss' scoped>
.showMapWrapper {
width: 500px;
height: 500px;
border: 1px solid #999;
}
</style>
大功告成~
補充知識:vant 中 AddressEdit 地址編輯 設置手機號格式校驗
使用AddressEdit 組件中的 tel-validator 手機號格式校驗API
添加 :tel-validator=“validator”
<VanAddressEdit :area-list="areaList" :address-info="addressInfo" :show-delete="Boolean(editId)" show-postal :is-saving="isSaving" :is-deleting="isDeleting" save-button-text="保存并使用" delete-button-text="刪除收貨地址" @save="onSave" @delete="onDelete" :tel-validator="validator" >
在methods中用正則設置需要的手機號格式
methods: {
validator(e) {
console.log(e);
let myreg1 = /^[1][3,4,5,7,8][0-9]{9}$/;
let myreg2 = /^[2][3,4,5,7,8][0-9]{6}$/;
if (!myreg1.test(e) && !myreg2.test(e)) {
console.log("手機號錯誤");
return false;
} else{
console.log('手機號正確')
return true
}
},
}
以上這篇vue-amap根據(jù)地址回顯地圖并mark的操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Nginx配置Vue項目,無法按路徑跳轉(zhuǎn)及刷新404的解決方案
這篇文章主要介紹了Nginx配置Vue項目,無法按路徑跳轉(zhuǎn)及刷新404的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
vue組件中節(jié)流函數(shù)的失效的原因和解決方法
這篇文章主要介紹了vue組件中節(jié)流函數(shù)的失效和解決方法,幫助大家更好的理解和學習vue框架,感興趣的朋友可以了解下2020-12-12
基于Ant-design-vue的Modal彈窗 封裝 命令式與Hooks用法
這篇文章主要給大家介紹了基于Ant-design-vue的Modal彈窗封裝命令式與Hooks用法,文中有詳細的代碼示例,具有一定的參考價值,感興趣的同學可以借鑒閱讀2023-06-06

