效果如下


·

引入地图qqmap

刚开始我是直接用 npm install qqmap,但是好像只有v1版本的,我需要用v2版本的,所以直接使用script标签加载API服务。

文件:/public/index.html

<script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=YOUR_KEY"></script>

v参数是引用的版本号,目前腾讯地图提供两个版本,分别是v=1,v=2.exp,推荐使用2.exp,可以获得最新最快的支持。key参数YOUR_KEY是Key鉴权中申请的key。

参考文档:# 腾讯位置服务API #

·

创建自定义组件

文件:/components/MapSelect.vue

<template><el-dialogtitle="选择地址":visible.sync="isShowDialog"width="1000px"top="50px"><div class="search"><el-input placeholder="请输入搜索地址信息" v-model="keyword" clearable><el-button slot="append" icon="el-icon-search" @click="searchAddress"></el-button></el-input></div><div id="mapContainer" style="width: 100%; height: 350px"></div><div class="address"><span>当前选点:</span><b>{{nowAddress ? (nowAddress.addressComponents.province + nowAddress.addressComponents.city + nowAddress.addressComponents.district + nowAddress.addressComponents.streetNumber) : '尚未选点'}}</b><el-button v-if="nowAddress" @click="selectAddress(nowAddress, 1)" type="text">【确认选择】</el-button></div><el-tablehighlight-current-row:data="nearPointList"height="300"style="width: 100%"class="table"><el-table-column prop="address" label="附近推荐地点"><template slot-scope="scope">{{scope.row.address}}{{scope.row.name}}</template></el-table-column><el-table-column label="操作" width="100" align="center"><template slot-scope="scope"><el-button @click.native.prevent="selectAddress(scope.row, 2)" type="text">确认选择</el-button></template></el-table-column></el-table></el-dialog>
</template>
<script>export default {data() {return {keyword: '', // 搜索关键词nearPointList: [], // 附近地址isShowDialog: false, // 是否显示弹窗markersArray: [],geocoder: null,nowAddress: null, // 选择的地点geocoderLocation: null,};},mounted() {},methods: {// 搜索地址searchAddress() {if (!this.keyword) {return this.$message.error("请输入搜索地址信息");}this.setLocationByAddress(this.keyword);},// 初始化地图initMap() {let that = this;let latLon = new qq.maps.LatLng(39.916527, 116.397128);var map = new qq.maps.Map(document.getElementById("mapContainer"), {zoom: 13,center: latLon,mapTypeId: qq.maps.MapTypeId.ROADMAP,});var listener = qq.maps.event.addListener(map, 'click', function(event) {that.setLocationByLatLng(event.latLng.getLat(),event.latLng.getLng());});// 经纬度解析类回调函数this.geocoder = new qq.maps.Geocoder({complete: function (result) {console.log(result.detail);that.nowAddress = result.detail;that.nearPointList = result.detail.nearPois;map.setCenter(result.detail.location);// 标记点let marker = new qq.maps.Marker({map: map,position: result.detail.location,});that.markersArray.push(marker);if (that.markersArray.length > 1) {for (let i = 0; i < that.markersArray.length - 1; i++) {that.markersArray[i].setMap(null); // 清除标记}}},});// 地址解析回调函数that.geocoderLocation = new qq.maps.Geocoder({complete: function (result) {// 查找附近的点let latLng = new qq.maps.LatLng(result.detail.location.lat,result.detail.location.lng);that.geocoder.getAddress(latLng);},});},// 选择地址事件selectAddress(item, type) {if(type === 1) {let addressInfo = item.addressComponents;this.$emit("on-select",addressInfo.province + addressInfo.city + addressInfo.district + addressInfo.streetNumber,item.location.lat,item.location.lng);this.isShowDialog = false;}if(type === 2) {this.$emit("on-select",item.address + item.name,item.latLng.lat,item.latLng.lng);this.isShowDialog = false;}},// 显示地图show() {this.isShowDialog = true;setTimeout(() => {this.keyword = '';this.initMap();})},// 根据地址信息进行定位setLocationByAddress(address) {setTimeout(() => {this.geocoderLocation.getLocation(address);})},// 根据经纬度进行定位setLocationByLatLng(lat, lng) {setTimeout(() => {let latLng = new qq.maps.LatLng(lat, lng);this.geocoder.getAddress(latLng);})},},
};
</script><style scoped lang="scss">
.search {margin-bottom: 15px;margin-top: -20px;
}
.address {margin-top: 15px;margin-bottom: 10px;.el-button {padding: 0;}
}
.table {.el-button {padding: 0;}
}
</style>

·

页面调用

<template><div><el-input placeholder="请选择地址" v-model="mainForm.address" readonly><el-button slot="append" icon="el-icon-location" @click="openMap()"></el-button></el-input><MapSelect ref="ms" @on-select="selectAddress" /></div>
</template><script>
import MapSelect from '@/components/Common/MapSelect'export default {components: {MapSelect},data() {return {mainForm: {address: '',lat: '',lng: '',},}},methods: {// 打开地图弹窗openMap() {this.$refs.ms.show();// 根据省市区设置初始值// this.$refs.ms.setLocationByAddress(this.mainForm.address);// 根据经纬度设置初始值this.$refs.ms.setLocationByLatLng(this.mainForm.lat, this.mainForm.lng);},// 地址选择后的回调函数selectAddress(address, lat, lng) {this.mainForm.address = address;this.mainForm.lat = lat;this.mainForm.lng = lng;},}
}
</script>

vue+elementUI 使用腾讯地图相关推荐

  1. vue中使用腾讯地图选择地址

    腾讯地图选择地址引发的一系列问题 2019/12/14更新 修复了自治区/直辖市无法选择的bug(比如上海之类的城市) 采用更加友好的方式来选择地址 新的文章链接:vue中使用腾讯地图选择地址(二) ...

  2. 在vue中使用腾讯地图绘制围栏功能

    首先,先看效果图 里面包含了自定义多边形绘制围栏.圆形绘制以及矩形等方式,以及清除围栏经纬度的方法和获取围栏经纬度的方法. 首先在项目的index.html中引入腾讯地图 <script cha ...

  3. 在vue中使用腾讯地图

    1. 引入腾讯地图 在 index.html 中插入 <script charset="utf-8" src="https://map.qq.com/api/glj ...

  4. vue项目使用腾讯地图获取定位

    一.注册成为腾讯地图开发者 https://lbs.qq.com/ 创建新秘钥 https://lbs.qq.com/dev/console/key/manage   根据页面提示填写相关信息 复制你 ...

  5. vue仅使用腾讯地图根据城市名称获取经纬度

    方式一: 下载插件 1.下载 npm install qqmap --save   1 2.使用 // 2.1引入 import maps from 'qqmap' export default { ...

  6. vue中加载腾讯地图(html形式)

    vue中引入腾讯地图,目前根据网上的方式有一种 qqmap组件,但是本人才疏学浅,未能探究如何使用,于是乎,就曲线救国,用另一种方式实现了,在vue中引入iframe,然后在iframe中引入html ...

  7. vue项目中加载使用腾讯地图

    在vue-cli脚手架搭建的vue项目里使用到了腾讯地图.看了腾讯地图的官方给出的API(https://lbs.qq.com/javascript_v2/index.html)敲了一遍代码,才有点感 ...

  8. vue PC 端使用腾讯地图定位

    vue PC 端使用腾讯地图定位 需求:希望网站显示当前城市 腾讯前端定位组件 解决的方法 定义一个文件,加载定位js 在vue页面中使用 需求:希望网站显示当前城市 腾讯前端定位组件 key的申请, ...

  9. html调用腾讯地图定位当前位置,vue web项目中调用腾讯地图API获取当前位置的经纬度...

    vue web项目中调用腾讯地图API获取当前位置的经纬度 vue web项目中调用腾讯地图API获取当前位置的经纬度 在main.js 中添加一下代码 import axios from 'axio ...

最新文章

  1. PHP之路——MySql查询语句
  2. 【NOIP2015模拟10.22】最小代价
  3. 根据 dba_errors 制定 数据库报警邮件
  4. Predicate接口练习之筛选满足条件数据
  5. Packet for query is too large
  6. linux离线安装docker,centos离线安装docker
  7. 小白设计模式:组合模式
  8. 【LOJ10034】图书管理(哈希表,字符串)
  9. HDU 4539 郑厂长系列故事——排兵布阵 —— 状压DP
  10. conda create出现连接问题_解决conda创建新环境慢 conda install 速度慢 报错问题
  11. java 图层,图层Layers的介绍
  12. Cannot resolve the name 'repository:auditing-attributes' to a(n) 'attribute grou
  13. Sinew探索金融衍生品领域,增强金融市场流动性
  14. 男生学计算机会计,男生学习会计专业好吗
  15. OSError: [WinError 1455] 页面文件太小,无法完成操作。 Error loading “D:\Anaconda\envs\pytorch-1.4\lib\site-package
  16. 夜晚网速变慢与网站服务器开机数量减少有关,电脑网速慢到底与什么有关?
  17. vue3后台管理系统(https://github.com/noob-Jp/my-admin-vue3)
  18. 计算机系统概论基本知识
  19. 算法笔记_110:第四届蓝桥杯软件类省赛真题(JAVA软件开发高职高专组部分习题)试题解答...
  20. 什么是utf8mb4和utf8mb3区别?

热门文章

  1. DAG最长路(最短路)
  2. linux c 宏编译,linux c代码批量宏展开
  3. 睿智的目标检测66——Pytorch搭建YoloV8目标检测平台
  4. python 修改文件只读,Python脚本去除文件的只读性操作
  5. 【云原生 • Kubernetes】一文掌握 k8s 包管理工具 Helm
  6. 如何使用POI导出excel表格,以及处理浏览器无法识别下载文件的问题
  7. lab-on-a-disc 实现血液中血细胞与血浆分离的一种方法
  8. 23 个机器学习开源项目,附源码
  9. 分布式事务(六):分布式事务案例
  10. 百度前端技术学院——DAY2