Vue組件之高德地圖地址選擇功能的實(shí)例代碼
注:本文基于上一篇文章【Vue-Cli 3.0 中配置高德地圖 】 ,采用直接引入高德 SDK 的方式來使用高德地圖api
一、效果圖

二、組件要實(shí)現(xiàn)的功能
1. 如果有傳入坐標(biāo)點(diǎn),則定位到坐標(biāo)點(diǎn)
2. 如果沒有傳入坐標(biāo)點(diǎn),則定位到當(dāng)前所在位置
3. 定位成功要在右側(cè)顯示經(jīng)緯度和地址
4. 可以通過拖動 標(biāo)記 來調(diào)整定位點(diǎn)
5. 標(biāo)記 拖動后,右側(cè)要顯示拖動后的經(jīng)緯度和地址
6. 點(diǎn)擊確定按鈕,返回最后的坐標(biāo)點(diǎn)和地名給父組件
三、 組件實(shí)現(xiàn)具體代碼
<template>
<div class="map-box" :style="{ width: width, height: height }">
<div id="amap" class="amap"></div>
<div class="detail">
<p>經(jīng)度:{{point ? point[0] : '-'}}</p>
<p>緯度:{{point ? point[1] : '-'}}</p>
<p>地址:{{address}}</p>
<button size="mini" class="btnmap" @click="commit">確定</button>
</div>
</div>
</template>
<script>
import AMap from 'AMap'
export default {
props: {
width: { type: String, default: '100%' },
height: { type: String, default: '400px' },
lnglat: {
type: Array,
validator: (value) => {
return value.length === 2
}
}
},
data () {
return { address: '', point: this.lnglat }
},
mounted () {
this.init(this.point)
},
methods: {
// 初始化
init (lnglat) {
// 地圖實(shí)例對象 (amap 為容器的id)
let amap = new AMap.Map('amap', {
resizeEnable: true,
zoom: 15
})
// 注入插件(定位插件,地理編碼插件)
amap.plugin(['AMap.Geolocation', 'AMap.Geocoder'])
// 定位
this.currentPosition(amap, lnglat)
},
currentPosition (map, lnglat) {
if (lnglat) {
// 有傳入坐標(biāo)點(diǎn),直接定位到坐標(biāo)點(diǎn)
map.setCenter(lnglat)
this.addMark(map, lnglat)
// 獲取地址
this.getAddress(lnglat)
} else {
// 沒有傳入坐標(biāo)點(diǎn),則定位到當(dāng)前所在位置
let geolocation = new AMap.Geolocation({
enableHighAccuracy: true,
timeout: 10000,
zoomToAccuracy: true,
buttonPosition: 'RB'
})
geolocation.getCurrentPosition((status, result) => {
if (status === 'complete') {
let points = [result.position.lng, result.position.lat]
map.setCenter(points) // 設(shè)置中心點(diǎn)
this.addMark(map, points) // 添加標(biāo)記
// 存下坐標(biāo)與地址
this.point = points
this.getAddress(points)
} else {
console.log('定位失敗', result)
}
})
}
},
// 添加標(biāo)記
addMark (map, points) {
let marker = new AMap.Marker({
map: map,
position: points,
draggable: true, // 允許拖動
cursor: 'move',
raiseOnDrag: true
})
marker.on('dragend', (e) => {
let position = marker.getPosition()
// 存下坐標(biāo)與地址
this.point = [position.lng, position.lat]
this.getAddress([position.lng, position.lat])
})
},
// 根據(jù)坐標(biāo)返回地址(逆地理編碼)
getAddress (points) {
let geocoder = new AMap.Geocoder({ radius: 1000 })
geocoder.getAddress(points, (status, result) => {
if (status === 'complete' && result.regeocode) {
this.address = result.regeocode.formattedAddress
}
})
},
commit () {
this.$emit('location', this.point, this.address)
}
}
}
</script>
<style lang="scss" scoped>
.map-box {
box-sizing: border-box;
background-color: #ddd;
padding: 15px;
&:after {
content: '';
display: block;
clear: both;
}
.amap, .detail {
float: left;
height: 100%;
}
.amap {
width: 75%;
}
.detail {
width: 25%;
background-color: #fff;
padding: 0 15px;
border-left: 1px solid #eee;
box-sizing: border-box;
word-wrap: break-word;
}
.btnmap {
width: 100%;
margin: 30px 0 0 0;
padding: 5px 0;
color: #fff;
cursor: pointer;
background-color: #409eff;
border: none;
border-radius: 3px;
&:hover {
background-color: #66b1ff;
}
}
}
</style>
四、調(diào)用組件
<template>
<div class="box">
<xmap width="700px" height="500px" :lnglat="[114.433703, 30.446243]" @location="location"></xmap>
</div>
</template>
<script>
import xmap from '@/components/map'
export default {
components: { xmap },
methods: {
location(point, address) {
alert(`坐標(biāo):${point[0]},${point[1]} - 地址:${address}`)
}
}
}
</script>
總結(jié)
以上所述是小編給大家介紹的Vue組件之高德地圖地址選擇功能的實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
相關(guān)文章
vue 根據(jù)數(shù)組中某一項(xiàng)的值進(jìn)行排序的方法
這篇文章主要介紹了vue 根據(jù)數(shù)組中某一項(xiàng)的值進(jìn)行排序的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
vue新建項(xiàng)目并配置標(biāo)準(zhǔn)路由過程解析
這篇文章主要介紹了vue新建項(xiàng)目并配置標(biāo)準(zhǔn)路由過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12
Vue中實(shí)現(xiàn)父子組件雙向數(shù)據(jù)流的三種方案分享
通常情況下,父子組件的通信都是單向的,或父組件使用props向子組件傳遞數(shù)據(jù),或子組件使用emit函數(shù)向父組件傳遞數(shù)據(jù),本文將嘗試講解Vue中常用的幾種雙向數(shù)據(jù)流的使用,需要的朋友可以參考下2023-08-08
簡單實(shí)現(xiàn)vue中的依賴收集與響應(yīng)的方法
這篇文章主要介紹了簡單實(shí)現(xiàn)vue中的依賴收集與響應(yīng)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02

