DDR爱好者之家 Design By 杰米
注:本文基于上一篇文章【Vue-Cli 3.0 中配置高德地图 】 ,采用直接引入高德 SDK 的方式来使用高德地图api
一、效果图
二、组件要实现的功能
1. 如果有传入坐标点,则定位到坐标点
2. 如果没有传入坐标点,则定位到当前所在位置
3. 定位成功要在右侧显示经纬度和地址
4. 可以通过拖动 标记 来调整定位点
5. 标记 拖动后,右侧要显示拖动后的经纬度和地址
6. 点击确定按钮,返回最后的坐标点和地名给父组件
三、 组件实现具体代码
<template>
<div class="map-box" :style="{ width: width, height: height }">
<div id="amap" class="amap"></div>
<div class="detail">
<p>经度:{{point "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) {
// 地图实例对象 (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) {
// 有传入坐标点,直接定位到坐标点
map.setCenter(lnglat)
this.addMark(map, lnglat)
// 获取地址
this.getAddress(lnglat)
} else {
// 没有传入坐标点,则定位到当前所在位置
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) // 设置中心点
this.addMark(map, points) // 添加标记
// 存下坐标与地址
this.point = points
this.getAddress(points)
} else {
console.log('定位失败', result)
}
})
}
},
// 添加标记
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()
// 存下坐标与地址
this.point = [position.lng, position.lat]
this.getAddress([position.lng, position.lat])
})
},
// 根据坐标返回地址(逆地理编码)
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>
四、调用组件
<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(`坐标:${point[0]},${point[1]} - 地址:${address}`)
}
}
}
</script>
总结
以上所述是小编给大家介绍的Vue组件之高德地图地址选择功能的实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2025年11月06日
2025年11月06日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
