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

2、添加標注點(此處以地圖的center為位置點添加)
模板:
<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數據,作為該點的介紹使用 ,可參照文檔自行決定是否添加
label:{
content:'欽匯園',
offset:[10,12]
},保存后結果如下圖 marker已經加載了

3、添加圓形區域?(此處依舊以中心點為圓心 半徑為100)
注意:添加圓形區域時,要在初始化插件里初始化"AMap.CircleEditor",否則會報錯
模板:
<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>
拓展:動態更改圓形區域的半徑,可用于設置范圍
此處我以“精度++”按鈕為例,每點擊一次半徑加10
data數據:
radius:100
增加事件:
addRadius(){
this.radius+=10;
}PS:添加其他覆蓋物,如折線,圖片,多邊形等,用法與此類似,參照官方文檔進行使用即可
效果圖如下:

3、使用插件
只用插件時,一定要在前面initAMapApiLoader里面進行初始化,否則會報錯
模板:
<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里添加插件數據:
plugin: [
{
pName: 'ToolBar',//工具條插件
position:'LB',
},
{
pName: 'MapType',//衛星與地圖切換
defaultType: 0,
showTraffic:true,//實時交通圖層
},
{
pName:'OverView',
//isOpen:true//鷹眼是否打開
},
{
pName:'Scale'
},
{
pName:'Geolocation',//定位插件
showMarker:false,
events:{
init(o){
//定位成功 自動將marker和circle移到定位點
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',//衛星與地圖切換
defaultType: 0,
showTraffic:true,//實時交通圖層
},
{
pName:'OverView',
//isOpen:true//鷹眼是否打開
},
{
pName:'Scale'
},
{
pName:'Geolocation',//定位插件
showMarker:false,
events:{
init(o){
//定位成功 自動將marker和circle移到定位點
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的使用方法”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。