這篇文章主要介紹vue中amap的使用方法,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

vue amap的使用方法:首先通過“vue init webpack vueamap”下載vue webpack的模板;然后使用“cnpm install vue-amap --save”安裝vue-amap;最后運(yùn)用此組件庫即可。
一、 down一個(gè)vue webpack的模板
vue init webpack vueamap
根據(jù)提示完成模板下載,此處我的項(xiàng)目中選擇router為yes 其他測(cè)試插件全為no? vueamap為文件夾名稱
模板下載后 安裝依賴
cnpm install
依賴安裝完成后 執(zhí)行開發(fā)環(huán)境
npm run dev
若提示在"localhost:8080"上查看效果,在瀏覽器上查看效果,若出現(xiàn)VUE效果 則模板下載成功
二、安裝vue-amap
安裝vue-amap
cnpm install vue-amap --save
安裝完成后,main.js文件中引入
import VueAMap from "vue-amap"; Vue.use(VueAMap);
初始化高德地圖,此處需要有一個(gè)KEY,可以到高德地圖平臺(tái)上去申請(qǐng).
初始化高德地圖的key與插件
VueAMap.initAMapApiLoader({
key: "e1dedc6bdd765d46693986ff7ff969f4",
plugin: [
"AMap.Autocomplete", //輸入提示插件
"AMap.PlaceSearch", //POI搜索插件
"AMap.Scale", //右下角縮略圖插件 比例尺
"AMap.OverView", //地圖鷹眼插件
"AMap.ToolBar", //地圖工具條
"AMap.MapType", //類別切換控件,實(shí)現(xiàn)默認(rèn)圖層與衛(wèi)星圖、實(shí)施交通圖層之間切換的控制
"AMap.PolyEditor", //編輯 折線多,邊形
"AMap.CircleEditor", //圓形編輯器插件
"AMap.Geolocation" //定位控件,用來獲取和展示用戶主機(jī)所在的經(jīng)緯度位置
],
uiVersion: "1.0"
});三、 使用
下面開始正式運(yùn)用此組件庫
注:后續(xù)所用到的配置并非全面配置,若有不懂或想詳細(xì)了解,
請(qǐng)移步vue-amap文檔:vue-amap文檔(https://elemefe.github.io/vue-amap/#/zh-cn/introduction/install)
文檔介紹比較簡(jiǎn)單,建議到高德官方查看參考手冊(cè)對(duì)照使用
高德參考手冊(cè):參考手冊(cè)(http://lbs.amap.com/api/javascript-api/reference/map)
1、構(gòu)建地圖
模板:
<div class="amap-wrapper"> <el-amap class="amap-box" vid="map" :zoom="zoom" :center="center"> </el-amap> </div>
data中數(shù)據(jù):
zoom:16, center:[121.406051,31.179695],
保存后,瀏覽器中運(yùn)行,效果圖如下:

2、添加標(biāo)注點(diǎn)(此處以地圖的center為位置點(diǎn)添加)
模板:
<div class="amap-wrapper"> <el-amap vid="amapDemo" :center="center" :zoom="zoom" class="amap-demo"> <el-amap-marker vid="marker" :position="center" :label="label" > </el-amap-marker> </el-amap> </div>
增加一條label數(shù)據(jù),作為該點(diǎn)的介紹使用 ,可參照文檔自行決定是否添加
label:{
content:'欽匯園',
offset:[10,12]
},保存后結(jié)果如下圖 marker已經(jīng)加載了

3、添加圓形區(qū)域?(此處依舊以中心點(diǎn)為圓心 半徑為100)
注意:添加圓形區(qū)域時(shí),要在初始化插件里初始化"AMap.CircleEditor",否則會(huì)報(bào)錯(cuò)
模板:
<div class="amap-wrapper"> <el-amap vid="amapDemo" :center="center" :zoom="zoom" class="amap-demo"> <el-amap-marker vid="marker" :position="center" :label="label" > </el-amap-marker> <el-amap-circle vid="circle" :center="center" :radius="radius" fill-opacity="0.2" strokeColor="#38f" strokeOpacity="0.8" strokeWeight="1" fillColor="#38f" > </el-amap-circle> </el-amap> </div>
拓展:動(dòng)態(tài)更改圓形區(qū)域的半徑,可用于設(shè)置范圍
此處我以“精度++”按鈕為例,每點(diǎn)擊一次半徑加10
data數(shù)據(jù):
radius:100
增加事件:
addRadius(){
this.radius+=10;
}PS:添加其他覆蓋物,如折線,圖片,多邊形等,用法與此類似,參照官方文檔進(jìn)行使用即可
效果圖如下:

3、使用插件
只用插件時(shí),一定要在前面initAMapApiLoader里面進(jìn)行初始化,否則會(huì)報(bào)錯(cuò)
模板:
<div class="amap-wrapper"> <el-amap class="amap-box" vid="map" :zoom="zoom" :center="center" :plugin="plugin"> <el-amap-marker vid="marker" :position="center" :label="label" > </el-amap-marker> <el-amap-circle vid="circle" :center="center" :radius="radius" fill-opacity="0.2" strokeColor="#38f" strokeOpacity="0.8" strokeWeight="1" fillColor="#38f" > </el-amap-circle> </el-amap> </div>
data里添加插件數(shù)據(jù):
plugin: [
{
pName: 'ToolBar',//工具條插件
position:'LB',
},
{
pName: 'MapType',//衛(wèi)星與地圖切換
defaultType: 0,
showTraffic:true,//實(shí)時(shí)交通圖層
},
{
pName:'OverView',
//isOpen:true//鷹眼是否打開
},
{
pName:'Scale'
},
{
pName:'Geolocation',//定位插件
showMarker:false,
events:{
init(o){
//定位成功 自動(dòng)將marker和circle移到定位點(diǎn)
o.getCurrentPosition((status, result) => {
console.log(result);
vm.center=[result.position.lng,result.position.lat]
});
}
}
}
]效果圖如下:

全部代碼如下:
<template>
<div>
<p>{{msg}}</p>
<button @click="addRadius">精度++</button>
<hr>
<div class="amap-wrapper">
<el-amap class="amap-box" vid="map"
:zoom="zoom"
:center="center"
:plugin="plugin">
<el-amap-marker vid="marker"
:position="center"
:label="label"
>
</el-amap-marker>
<el-amap-circle vid="circle"
:center="center"
:radius="radius"
fill-opacity="0.2"
strokeColor="#38f"
strokeOpacity="0.8"
strokeWeight="1"
fillColor="#38f"
>
</el-amap-circle>
</el-amap>
</div>
</div>
</template>
<script>
export default {
name:'home',
data(){
let vm=this;
return{
msg:'vue-amap demo',
zoom:16,
center:[121.406051,31.179695],
label:{
content:'欽匯園',
offset:[10,12]
},
radius:100,
plugin: [
{
pName: 'ToolBar',//工具條插件
position:'LB',
},
{
pName: 'MapType',//衛(wèi)星與地圖切換
defaultType: 0,
showTraffic:true,//實(shí)時(shí)交通圖層
},
{
pName:'OverView',
//isOpen:true//鷹眼是否打開
},
{
pName:'Scale'
},
{
pName:'Geolocation',//定位插件
showMarker:false,
events:{
init(o){
//定位成功 自動(dòng)將marker和circle移到定位點(diǎn)
o.getCurrentPosition((status, result) => {
console.log(result);
vm.center=[result.position.lng,result.position.lat]
});
}
}
}
]
}
},
methods:{
addRadius(){
this.radius+=10;
}
}
}
</script>
<style scoped>
hr{
border-color: red;
border-style: dashed;
}
.amap-wrapper{
height: 300px;
}
</style>以上是“vue中amap的使用方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站欄目:vue中amap的使用方法-創(chuàng)新互聯(lián)
路徑分享:http://chinadenli.net/article30/dpgopo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站改版、軟件開發(fā)、網(wǎng)站設(shè)計(jì)公司、做網(wǎng)站、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容