vue-amap組件怎么在vue項(xiàng)目中使用?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
成都創(chuàng)新互聯(lián)長期為上千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為代縣企業(yè)提供專業(yè)的網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì),代縣網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
首先
npm install -S vue-amap
然后在 main.js
import VueAMap from 'vue-amap'; //注意不要和 AMap原始名稱覆蓋 Vue.use(VueAMap); // 初始化vue-amap VueAMap.initAMapApiLoader({ // 高德的key key: 'you key', // 插件集合 plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor','AMap.Geolocation'], v: '1.4.4' });
map.vue文件
其中有個(gè)BUS.js,是基于觀察者模式的發(fā)布訂閱封裝
<template> <div class="_map"> <div class="amap-page-container"> <el-amap-search-box class="search-box" :search-option="searchOption" :on-search-result="onSearchResult" ></el-amap-search-box> <el-amap ref="map" vid="amapDemo" :plugin="plugin" :zoom="zoom" :center="center" class="amap-demo" :events="events"> <el-amap-marker vid="component-marker" :position="makerConf.position" :content="makerConf.content" ></el-amap-marker> </el-amap> </div> <div class="adrs"> <ul> <li class="" v-for="(item,index) in list" :key="index" :class="currIndex == index ? 'active':''" @click="select(item,index)"> <p class="address">{{item.address}}</p> <p class="nm">{{item.name}}</p> </li> </ul> </div> </div> </template> <style> .amap-page-container{ height: 300px; position: relative; } .search-box { position: absolute !important; top: 25px; left: 20px; z-index: 200 !important; } .amap-demo { height: 300px; } .amap-logo { display: none; } .amap-copyright { bottom:-100px; display: none; } .amap-scalecontrol{ bottom: 4px !important; } .amap-geolocation-con{ bottom: 30px !important; z-index: 199 !important; } ul li.active{ color: deeppink; } </style> <script> export default { name: 'amap-page', components: {}, data() { var me = this; me.city = me.city || '武漢'; return { list:[], currIndex:0, zoom: 16, center: [114.397169, 30.50576], events:{ init: (o) => { o.setCity(me.city,result => { console.log("----------setCity",result); if(result && result.length > 0){ me.zoom = 16; me.makerConf.position = result; me.getList(result); } }); //去掉logo document.getElementsByClassName("amap-logo")[0].style.display = "none"; }, "dragend":function(e){ //console.log("dragging",e,this.getCenter()); var point = this.getCenter(); var pos = [point.lng,point.lat]; me.makerConf.position = [point.lng,point.lat]; me.getList(pos); } }, makerConf: { position: [114.397169, 30.50576], content:"" }, searchOption: { city: me.city, citylimit: true }, plugin:[ 'ToolBar', 'Scale', { pName: 'Geolocation', events: { init(o) { }, complete:function(result){ //定位成功 var address = result.formattedAddress var point = result.position; var obj = { address:address, name:"", location:point } me.list = [obj]; me.makerConf.position = [point.lng,point.lat]; }, error:function(){ } } } ] }; }, created(){ var me = this; }, mounted(){ }, methods: { select:function(item,index){ var me = this; me.currIndex = index; var point = item.location; me.makerConf.position = [point.lng,point.lat]; me.center = [point.lng,point.lat]; }, //this.$refs.map.$$getCenter() getList:function(result){ //獲取列表 var me = this; me.$Geocoder({ lnglatXY:result, success:function(res){ if(res.regeocode && res.regeocode.pois){ me.list = res.regeocode.pois; }else{ me.list = []; } }, error:function(res){ me.list = []; } }); }, onSearchResult(pois) { //搜索 let latSum = 0; let lngSum = 0; var me = this; var mymap = me.$refs.map.$$getInstance(); if (pois && pois.length > 0) { //如果長度為1則無需轉(zhuǎn)化 var poi = pois[0]; var lng = poi["lng"]; var lat = poi["lat"]; me.center = [lng, lat]; me.makerConf.position = [lng, lat]; //me.makerConf.content = poi.name; me.list = pois; }else{ me.list = []; } }, $Geocoder(options){ //將坐標(biāo)點(diǎn)轉(zhuǎn)化為,詳細(xì)地址 options = options || {}; if(AMap){ AMap.plugin(['AMap.Geocoder'], () => { const geocoder = new AMap.Geocoder({ radius: options.radius || 1000, extensions: options.extensions || "all" }) var lnglatXY = options.lnglatXY || [114.397169, 30.50576]; //已知點(diǎn)坐標(biāo) geocoder.getAddress(lnglatXY, function(status, result) { if (status === 'complete' && result.info === 'OK') { options.success && options.success(result); }else{ options.error && options.error(status,result); } }); }); } } }, "watch":{ list:function(){ this.currIndex = 0; } } }; /* me.$Geocoder({ lnglatXY:[center.lng, center.lat], success:function(res){ console.log(res); } }); * * */ </script>
bus.js
let instance = null; class EventBus { constructor() { if (!instance) { this.events = {}; instance = this; } return instance; } $emit(event, message) { if (!this.events[event]) return; const callbacks = this.events[event]; for (let i = 0, l = callbacks.length; i < l; i++) { const callback = callbacks[i]; callback.call(this, message); } } $on(event, callback) { if (!this.events[event]) this.events[event] = []; this.events[event].push(callback); } } export default new EventBus();
效果圖
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
網(wǎng)站名稱:vue-amap組件怎么在vue項(xiàng)目中使用
網(wǎng)站URL:http://www.rwnh.cn/article26/jdjpcg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、企業(yè)建站、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)