VUE组件 之 高德地图地址选择
注:本文基于上一篇文章【 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 ? 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) { // 地图实例对象 (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组件 之 高德地图地址选择的更多相关文章
- vue+vant ui+高德地图的选址组件
首先在index.html引入高德地图的js <script src="https://webapi.amap.com/maps?v=1.4.14&key=你的key" ...
- 在vue中使用高德地图vue-amap
1.安装 vue-amap我安装指定版本0.5.10的版本 npm i --save vue-amap@0.5.10 2.main.js中的配置 key申请地址教程:https://lbs.amap. ...
- 在vue中使用高德地图开发,以及AMap的引入?
百度引入BMap ,一个import 即可,可AMap 却报AMap is not difined ? 1.首先在 externals: { "BMap": "BMap& ...
- vue.js 使用高德地图
1.获取key值 注册成为高德开发者需要分三步: 第一步,注册高德开发者: 第二步,去控制台创建应用: 第三步,获取Key 2.修改配置文件 webpack.base.conf.js externa ...
- vue 里面引入高德地图
效果图: 实现: 一:引入 高德,web-sdk (两种方式) 1:在html 中引入(我用的这一种) <script type="text/javascript" src= ...
- 如何在vue里面调用高德地图
1.修改webpac.base.conf.js文件 与module同一级添加 externals: { 'AMap': 'AMap', 'AMapUI': 'AMapUI' }配置. 然后在index ...
- vue+ElementUI+高德API地址模糊搜索(自定义UI组件)
开发环境描述: Vue.js ElementUI 高德地图API 需求描述: 在新增地址信息的时候,我们需要根据input输入的关键字调用地图的输入提示API,获取到返回的数据,并根据这些数据生成下拉 ...
- VUE组件汇总
内容 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 UI组件 element ★13489 - 饿了么出品的Vue2的web UI工具套件 Vux ★8133 - 基于Vue和 ...
- Vue-Cli 3.0 中配置高德地图
vue 中使用高德地图有两种方式 一.vue-amap 组件 官网: https://elemefe.github.io/vue-amap/#/ 开始的时候是打算用这个组件做地图功能的,但是尝试之后存 ...
随机推荐
- ThinkPHP的视图和模板
简单来说一个控制器对应一个视图,一个方法对应一个模板下面我们直接上图. 二.给模板赋值 给模板赋值在这里用到了assign()这个函数,assign()函数第一个参数为给这个值自定义名称,第二个参数则 ...
- [TimLinux] django context_processor介绍
1. context django里面 render 函数,HttpResponse,都有一个参数,context={},这个参数用于将视图层处理得到的数据传递到模板层. 2. context_pro ...
- cf1119d Frets On Fire 前缀和+二分
题目:http://codeforces.com/problemset/problem/1119/D 题意:给一个数n,给出n个数组的第一个数(a[0]=m,a[1]=m+1,a[2]=m+2,... ...
- SPOJ Distanct Substrings(求不同子串的数量)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- HDU1885 Key Task
The Czech Technical University is rather old — you already know that it celebrates 300 years of its ...
- Openfiler存储搭建
说明: Openfiler是一个基于Linux的开源免费网络存储管理操作系统,通过WEB界面对存储磁盘进行管理,支持iSCSI.NFS 等网络存储协议. 目前最新版本:openfileresa-2.9 ...
- httpBasic 认证的URL访问
httpBasic 认证 要在发送请求的时候添加HTTP Basic Authentication认证信息到请求中,有两种方法: 1.在请求头中添加Authorization: Authorizati ...
- .Net Core 3.0 以及其前版本编写自定义主机,以允许本机程式(转载)
像所有的托管代码一样,.NET Core 应用程序也由主机执行. 主机负责启动运行时(包括 JIT 和垃圾回收器等组件)和调用托管的入口点. 托管 .NET Core 运行时是高级方案,在大多数情况下 ...
- Weed3 for java 新的微型ORM框架
Weed3,微型ORM框架(支持:java sql,xml sql,annotation sql:存储过程:事务:缓存:监听:等...) 05年时开发了第一代: 08年时开发了第二代,那时候进入互联网 ...
- go1.13 mod 实践和常见问题
实践建议 0,go mod 要求所有依赖的 import path 的path 以域名开头,如果现有项目转1.13的go mod 模式,且不是以域名开头则需要修改. eg: code.be.mingb ...