vue 中 使用腾讯地图 (动态引用腾讯地图及使用签名验证)

在这里插入图片描述
在这里插入图片描述

在设置定位的时候使用 腾讯地图 选择地址
在 mounted中引入腾讯地图:
this.website.mapKey 为地图的 key

// 异步加载腾讯地图APIconst script = document.createElement('script');script.type = 'text/javascript';script.src = 'https://map.qq.com/api/js?v=2.exp&key='+this.website.mapKey+'&callback=init';document.body.appendChild(script);// 回调函数,初始化地图window.init = () => {this.initMap();this.$nextTick(()=>{if(this.map){//   // 监听中心点变化事件,更新marker的位置this.listener=qq.maps.event.addListener(this.map, 'dragend', ()=>this.centerChanged());}})};

在 destroyed 卸载

 destroyed(){let scripts = document.querySelectorAll('script');// 遍历所有找到的<script>元素并移除它们scripts.forEach((script)=> {let src=script.getAttribute('src');if(src&&src.indexOf('qq')>=0){script.parentNode.removeChild(script);}});qq.maps.event.removeListener(this.listener)},

弹框组件代码为:

<template><el-dialog title="设置定位":visible.sync="dialogVisible"width="900px":before-close="handleClose":modal-append-to-body="true":append-to-body="true":close-on-click-modal="false"v-dialogdragclass="common-dialog"><div class="clearfix"><div class="pull-left near-box"><el-input v-model="keyword" @change="changeKeyword"><el-button slot="append" icon="el-icon-search" @click="searchNear"></el-button></el-input><ul class="location-list"><li v-for="(item,index) in nearList" :key="index" :class="selectedIndex==index?'location-active':''"><div @click="handleSelect(item,index)"><div class="location-title">{{item.title}}</div><span class="location-address">{{item.address}}</span></div></li></ul></div><div class="pull-right map-box"><div id="container"></div></div></div><span slot="footer" class="dialog-footer"><el-button @click="handleClose">取 消</el-button><el-button type="primary" @click="submitAction">确 定</el-button></span></el-dialog>
</template>
<script>import {mapGetters} from "vuex";import {getAddressByLat,searchByKeyword} from '@/api/address'export default {props:{form:{type: Object}},data(){return {selectedIndex:'',keyword: '山东省青岛市',dialogVisible : true,mapZoom: 15,pitch: 0,addressInfo: this.form,mapCenter: {adcode: 370203,city: "青岛市",district: "市北区",ip: "111.17.222.181",lat: 36.08743,lng: 120.37479,nation: "中国",province: "山东省"},nearList:[],map:null,//地图markerLayer:null,listener:null}},mounted(){if(this.form.lat&&this.form.lng){this.mapCenter={...this.form,city:this.form.cityName};this.keyword=this.form.provinceName+this.form.cityName+this.form.areaName+this.form.address.substring(0,64)} else if(this.location.lat){this.mapCenter = {...this.location};if(this.location.province&&this.location.city){this.keyword=(this.location.province+this.location.city).substring(0,64)}}// 异步加载腾讯地图APIconst script = document.createElement('script');script.type = 'text/javascript';script.id = 'qqMap';script.name = 'qqMap';script.src = 'https://map.qq.com/api/js?v=2.exp&key='+this.website.mapKey+'&callback=init';document.body.appendChild(script);// 回调函数,初始化地图window.init = () => {this.initMap();this.$nextTick(()=>{if(this.map){//   // 监听中心点变化事件,更新marker的位置this.listener=qq.maps.event.addListener(this.map, 'dragend', ()=>this.centerChanged());}})};},destroyed(){let scripts = document.querySelectorAll('script');// 遍历所有找到的<script>元素并移除它们scripts.forEach((script)=> {let src=script.getAttribute('src');if(src&&src.indexOf('qq')>=0){script.parentNode.removeChild(script);}});qq.maps.event.removeListener(this.listener)},computed: {...mapGetters(["location"]),},methods:{//初始化地图initMap(){let element=document.getElementById('container');//定义地图中心点坐标let center=new qq.maps.LatLng(this.mapCenter.lat,this.mapCenter.lng);//定义map变量,调用 TMap.Map() 构造函数创建地图this.map = new qq.maps.Map(element, {pitch: this.pitch,center: center,//设置地图中心点坐标zoom:this.mapZoom,   //设置地图缩放级别});// 创建一个位于地图中心点的markerthis.markerLayer = new qq.maps.Marker({map: this.map,position: center});if(this.keyword){this.getAddressByKeyword( this.mapCenter)}},centerChanged(){this.mapCenter=this.map.getCenter();this.getLocationByLat()},//当前选择handleSelect(item,index){this.selectedIndex=index;this.mapCenter={...item,lat:item.location.lat,lng:item.location.lng,};this.map.setCenter(new qq.maps.LatLng(item.location.lat,item.location.lng));this.markerLayer.setPosition(new qq.maps.LatLng(item.location.lat,item.location.lng))// this.getLocationByLat()},changeKeyword(val){this.keyword=val;},searchNear(){this.mapCenter={};this.getAddressByKeyword()},getLocationByLat(){getAddressByLat({location:`${this.mapCenter.lat},${this.mapCenter.lng}`,key:this.website.mapKey,}).then(res=>{this.keyword=res.result.address;this.getAddressByKeyword(res.result)})},//根据关键字查找地址列表//https://lbs.qq.com/service/webService/webServiceGuide/webServiceSuggestiongetAddressByKeyword(latInfo){let params={keyword:this.keyword,region:this.mapCenter.city?this.mapCenter.city:'',policy:1,page_size:20,page_index:1,address_format:'short',key:this.website.mapKey,};if(this.mapCenter.lat&&this.mapCenter.lat!=-1&&this.mapCenter.lng&&this.mapCenter.lng!=-1){params.location=`${this.mapCenter.lat},${this.mapCenter.lng}`}searchByKeyword(params).then(res=>{this.nearList=res.data;let first=res.data&&res.data[0]?res.data[0]:'';if(first){this.selectedIndex=0;if(!params.location){let lat=first.location.lat;let lng=first.location.lng;this.mapCenter={...first,lat:lat,lng:lng};this.map.setCenter(new qq.maps.LatLng(lat,lng))}} else if(latInfo){let obj={...latInfo.ad_info,...latInfo.location,address:latInfo.address,title:latInfo.formatted_addresses&&latInfo.formatted_addresses.recommend?latInfo.formatted_addresses.recommend:latInfo.address};this.mapCenter=obj;this.nearList=[obj];this.map.setCenter(new qq.maps.LatLng(this.mapCenter.lat,this.mapCenter.lng))}this.markerLayer.setPosition(new qq.maps.LatLng(this.mapCenter.lat,this.mapCenter.lng))})},handleClose(){this.dialogVisible=false;this.$emit('closeDialog',false)},submitAction(){if(!this.keyword){this.$message.error('请输入关键字查询/或拖动地图查找');return false}this.$emit('changeMapLocation', this.selectedIndex>=0&&this.nearList[this.selectedIndex]?this.nearList[this.selectedIndex]:this.mapCenter);this.handleClose()}}}
</script>
<style lang="scss" scoped>@import "@/styles/variables";.common-dialog {/deep/.el-dialog__body{padding:20px 30px;}.el-input__inner{height:36px;line-height: 36px;}}.near-box{width:300px;height:500px;}.map-box{width:calc(100% - 320px);height:500px;margin:0;padding:0}#container{width:100%;height:100%;}/deep/ .el-input{min-width: auto;}.location-list{list-style: none;margin: 10px 0 0;padding:0;max-height: 460px;border:1px solid $color-border-light;overflow-y: auto;}.location-list li{list-style: none;padding:5px;border-bottom:1px solid $color-border-light;cursor: pointer;&:last-child{border-bottom: none;}}.location-list li.location-active{background-color: $color-primary;.location-title,.location-address{color:#fff;}}.location-title{font-size: 14px;color:$color-text-dark;font-weight: bold;}.location-address{font-size: 12px;color: $color-text-secondary;transform: scale(0.85);}
</style>

以逆地址解析为例写法为:

import request from "@/axios";
//逆地址解析
export const getAddressByLat = (params) =>{return request.jsonp('/ws/geocoder/v1', {output: "jsonp",...params})
}

axios 调用 jsonp 方法

import axios from 'axios';
import {serialize} from '@/util/util';
import {Message} from 'element-ui';
axios.jsonp = function(url,data){if(!url) throw new Error('接口地址错误')function sortObjectByKeys(obj) {return Object.keys(obj).sort().reduce((sortedObj, key) => {sortedObj[key] = obj[key];return sortedObj;}, {});}const callback = 'CALLBACK' + Math.random().toString().substr(9,18)const time=Date.now();let newData=sortObjectByKeys({...data,callback,time});let sign=md5(`${url}?${serialize(newData)}YOUR_SK`);const JSONP = document.createElement('script')JSONP.setAttribute('type','text/javascript')const headEle = document.getElementsByTagName('head')[0];JSONP.src = `https://apis.map.qq.com${url}?${serialize(newData)}&sig=${sign}`;return new Promise( (resolve) => {window[callback] = r => {if(r.status!='0'){Message({message: r.message,type: 'warning'});}resolve(r)headEle.removeChild(JSONP)delete window[callback]}headEle.appendChild(JSONP)})
}
export default axios;

YOUR_SK为腾讯地图签名验证时SK。 见下图:
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1472324.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

昇思25天学习打卡营第17天|GAN图像生成

模型简介 GAN模型的核心在于提出了通过对抗过程来估计生成模型这一全新框架。在这个框架中&#xff0c;将会同时训练两个模型——捕捉数据分布的生成模型G和估计样本是否来自训练数据的判别模型D 。 在训练过程中&#xff0c;生成器会不断尝试通过生成更好的假图像来骗过判别…

昇思25天学习打卡营第8天|MindSpore保存与加载(保存和加载MindIR)

在MindIR中&#xff0c;一个函数图&#xff08;FuncGraph&#xff09;表示一个普通函数的定义&#xff0c;函数图一般由ParameterNode、ValueNode和CNode组成有向无环图&#xff0c;可以清晰地表达出从参数到返回值的计算过程。在上图中可以看出&#xff0c;python代码中两个函…

Python面试宝典第6题:有效的括号

题目 给定一个只包括 (、)、{、}、[、] 这些字符的字符串&#xff0c;判断该字符串是否有效。有效字符串需要满足以下的条件。 1、左括号必须用相同类型的右括号闭合。 2、左括号必须以正确的顺序闭合。 3、每个右括号都有一个对应的相同类型的左括号。 注意&#xff1a;空字符…

搞了个 WEB 串口终端,简单分享下

每次换电脑总要找各种串口终端软件&#xff0c;很烦。 有的软件要付费&#xff0c;有的软件要注册&#xff0c;很烦。 找到免费的&#xff0c;还得先下载下来&#xff0c;很烦。 开源的软件下载速度不稳定&#xff0c;很烦。 公司电脑有监控还得让 IT 同事来安装&#xff0…

后端之路——最规范、便捷的spring boot工程配置

一、参数配置化 上一篇我们学了阿里云OSS的使用&#xff0c;那么我们为了方便使用OSS来上传文件&#xff0c;就创建了一个【util】类&#xff0c;里面有一个【AliOSSUtils】类&#xff0c;虽然本人觉得没啥不方便的&#xff0c;但是黑马视频又说这样还是存在不便维护和管理问题…

生态系统NPP及碳源、碳汇模拟技术教程

原文链接&#xff1a;生态系统NPP及碳源、碳汇模拟技术教程https://mp.weixin.qq.com/s?__bizMzUzNTczMDMxMg&mid2247608293&idx3&sn2604c5c4e061b4f15bb8aa81cf6dadd1&chksmfa826602cdf5ef145c4d170bed2e803cd71266626d6a6818c167e8af0da93557c1288da21a71&a…

FPGA问题

fpga 问题 ep2c5t144 开发板 第一道坎&#xff0c;安装软件&#xff1b;没有注册&#xff0c;无法产生sop文件&#xff0c;无法下载 没有相应的库的quartus ii版本&#xff0c;需要另下载 第二道坎&#xff0c;模拟器的下载&#xff0c;安装&#xff1b; 第三道&#xff0c;v…

在window上搭建docker

1、打开Hyper-V安装 在地址栏输入控制面板&#xff0c;然后回车 勾选Hyper-V安装&#xff0c;如果没有找到Hyper-V&#xff0c;那么请走第2步 2、如果没有Hyper-V(可选&#xff09;第一步无法打开 家庭版本需要开启Hyper-V 创建一个文本文档&#xff0c;后缀名称为.bat.名称…

油猴Safari浏览器插件:Tampermonkey for Mac 下载

Tampermonkey 是一个强大的浏览器扩展&#xff0c;用于运行用户脚本&#xff0c;这些脚本可以自定义和增强网页的功能。它允许用户在网页上执行各种自动化任务&#xff0c;比如自动填写表单、移除广告、改变页面布局等。适用浏览器&#xff1a; Tampermonkey 适用于多数主流浏览…

第二周:计算机网络概述(下)

一、计算机网络性能指标&#xff08;速率、带宽、延迟&#xff09; 1、速率 2、带宽 3、延迟/时延 前面讲分组交换的时候介绍了&#xff0c;有一种延迟叫“传输延迟”&#xff0c;即发送一个报文&#xff0c;从第一个分组的发送&#xff0c;到最后一个分组的发送完成的这段时…

PyQT: 开发一款ROI绘制小程序

在一些基于图像或者视频流的应用中&#xff0c;比如电子围栏/客流统计等&#xff0c;我们需要手动绘制一些感兴趣&#xff08;Region of Interest&#xff0c;简称ROI&#xff09;区域。 在这里&#xff0c;我们基于Python和PyQt5框架开发了一款桌面应用程序&#xff0c;允许用…

Renesas R7FA8D1BH (Cortex®-M85)串口应用总结

目录 概述 1 软硬件 1.1 软硬件环境信息 1.2 开发板信息 1.3 调试器信息 2 FSP和KEIL配置串口 2.1 配置参数 2.2 生成基于Keil的软件架构 3 FSP代码 3.1 FSP中UART接口函数 3.2 案例代码介绍 3.3 案例代码存在的问题 4 UART代码实现 4.1 功能函数介绍 4.2 完整…

如何在Qt使用uchardet库

如何在 Qt 中使用 uchardet 库 文章目录 如何在 Qt 中使用 uchardet 库一、简介二、uchardet库的下载三、在Qt中直接调用四、编译成库文件后调用4.1 编译工具下载4.2 uchardet源码编译4.3 测试编译文件4.4 Qt中使用 五、一些小问题5.1 测试文件存在的问题5.2 uchardet库相关 六…

嵌入式学习——硬件(UART)——day55

1. UART 1.1 定义 UART&#xff08;Universal Asynchronous Receiver/Transmitter&#xff0c;通用异步收发器&#xff09;是一种用于串行通信的硬件设备或模块。它的主要功能是将数据在串行和并行格式之间进行转换。UART通常用于计算机与外围设备或嵌入式系统之间的数据传输。…

【Linux系统编程】深入剖析:四大IO模型机制与应用(阻塞、非阻塞、多路复用、信号驱动IO 全解读)

目录 概述&#xff1a; 1. 阻塞IO (Blocking IO) 2. 非阻塞IO (Non-blocking IO) 3. IO多路复用 (I/O Multiplexing) 4. 信号驱动IO (Signal-driven IO) 阻塞式IO 非阻塞式IO 信号驱动IO&#xff08;Signal-driven IO&#xff09; 信号IO实例&#xff1a; IO多路复用…

nginx 搭理禅道

1.安装nginx。 2.安装禅道。 3.nginx 配置文件 location /zentao/ { proxy_pass http://192.168.100.66/zentao/;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-F…

Labview_Workers5.0 学习笔记

1.Local Request 个人理解该类型的请求针对自身的&#xff0c;由EHL或者MHL到该vi的MHL中。 使用快速放置快捷键"Ctrl9"创建方法如下&#xff1a; 创建后的API接口命名均为rql开头&#xff0c;并且在所选main.vi中的MHL创建对应的条件分支。 此时使用该API函数就…

巴图自动化PN转Modbus RTU协议转换网关模块快速配置

工业领域中常用的通讯协议有&#xff1a;Profinet协议&#xff0c;Modbus协议&#xff0c;ModbusTCP协议&#xff0c;Profibus协议&#xff0c;Profibus DP协议&#xff0c;EtherCAT协议&#xff0c;EtherNET协议&#xff0c;CAN&#xff0c;CanOpen等&#xff0c;它们在自动化…

78110A雷达信号模拟软件

78110A雷达信号模拟软件 78110A雷达信号模拟软件(简称雷达信号模拟软件)主要用于模拟产生雷达发射信号和目标回波信号&#xff0c;软件将编译生成的雷达信号任意波数据下载到信号发生器中&#xff0c;主要是1466-V矢量信号发生器&#xff0c;可实现雷达信号模拟产生。软件可模…