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

devtools/2024/9/19 23:09:41/ 标签: vue.js, javascript, 前端

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

在设置定位的时候使用 腾讯地图 选择地址
在 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.ppmy.cn/devtools/58230.html

相关文章

QT C++(QT控件 QLabel,QLCDNumber,QProgressBar,QCalendarWidget)

文章目录 1. QLabel2. QLCDNumber3. QProgressBar4. QCalendarWidget 1. QLabel QLabel常见属性&#xff1a; text&#xff1a;QLabel中的文本textFormat&#xff1a;文本中的格式 Qt::PlainText:纯文本Qt::RichText:富文本&#xff0c;支持html标签Qt::MarkdownText markdow…

本地部署 SenseVoice - 阿里开源语音大模型

本地部署 SenseVoice - 阿里开源语音大模型 1. 创建虚拟环境2. 克隆代码3. 安装依赖模块4. 启动 WebUI5. 访问 WebUI 1. 创建虚拟环境 conda create -n sensevoice python3.11 -y conda activate sensevoice 2. 克隆代码 git clone https://github.com/FunAudioLLM/SenseVoic…

谈面向任务的多轮对话系统(TOD)

面向任务对话系统&#xff08;Task-Oriented Dialogue (TOD) Systems)主要是为解决特定任务的&#xff0c;比如订票任务&#xff08;订机票&#xff0c;电影票等&#xff09;&#xff0c;预定饭店等。这种对话往往需要多轮对话才能够完成。 多轮对话的例子 客户预定一个餐厅的…

关于GIS的概念方面在前端编程中的理解

关于GIS的概念方面在前端编程中的理解 一. 什么是gis二. 关于地球的建模(了解)三. GIS坐标系表现形式四.GIS的数据4.1 矢量数据4.2 栅格数据4.3 矢量数据和栅格数据的不同 一. 什么是gis 地理坐标系统&#xff0c;其目的就是通过地理坐标系可以确定地球上任何一点的位置。 二. …

《梦醒蝶飞:释放Excel函数与公式的力量》8.3 COUNTBLANK函数

8.3 COUNTBLANK函数 在数据处理和分析中&#xff0c;我们经常需要识别和统计数据集中的空白单元格。COUNTBLANK函数是Excel中用于统计某个范围内空白单元格数量的强大工具。 8.3.1 函数简介 COUNTBLANK函数用于统计指定范围内的空白单元格数量。这在数据清洗、数据完整性检查…

【笔记】记一次在linux上通过在线安装mysql报错 CentOS 7 的官方镜像已经不再可用的解决方法+mysql配置

报错&#xff08;恨恨恨恨恨恨恨&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff09;&#xff1a; [rootlocalhost ~]# sudo yum install mysql-server 已加载插件&#xff1a;fastestmirror, langpacks Determining fastest mirrors Could not retrie…

回溯算法-以医院信息管理系统为例

1.回溯算法介绍 1.来源 回溯算法也叫试探法&#xff0c;它是一种系统地搜索问题的解的方法。 用回溯算法解决问题的一般步骤&#xff1a; 1、 针对所给问题&#xff0c;定义问题的解空间&#xff0c;它至少包含问题的一个&#xff08;最优&#xff09;解。 2 、确定易于搜…

hdu物联网硬件实验2 GPIO亮灯

学院 班级 学号 姓名 日期 成绩 实验题目 GPIO亮灯 实验目的 点亮三个灯闪烁频率为一秒 硬件原理 无 关键代码及注释 const int ledPin1 GREEN_LED; // the number of the LED pin const int ledPin2 YELLOW_LED; const int ledPin3 RED…

Linux Mac 安装Higress 平替 Spring Cloud Gateway

Linux Mac 安装Higress 平替 Spring Cloud Gateway Higress是什么?传统网关分类Higress定位下载安装包执行安装命令执行脚本 安装成功打开管理界面使用方法configure.shreset.shstartup.shshutdown.shstatus.shlogs.sh Higress官网 Higress是什么? Higress是基于阿里内部的…

Spring Boot整合Druid:轻松实现SQL监控和数据库密码加密

文章目录 1 引言1.1 简介1.2 Druid的功能1.3 竞品对比 2 准备工作2.1 项目环境 3 集成Druid3.1 添加依赖3.2 配置Druid3.3 编写测试类测试3.4 访问控制台3.5 测试SQL监控3.6 数据库密码加密3.6.1 执行命令加密数据库密码3.6.2 配置参数3.6.3 测试 4 总结 1 引言 1.1 简介 Dru…

【FreeRTOS】freeRTOS的版本号在哪个源文件定义

在task.h中定义 可以通过宏 tskKERNEL_VERSION_NUMBER 找到&#xff0c; 具体如下图&#xff1a;记录一下

51单片机———LED点阵屏显示图形动画

单片机上的一小块屏幕就是LED点阵屏&#xff0c;与数码管一样&#xff0c;内部由LED灯组成&#xff0c;只是点阵屏使用的LED灯更多&#xff0c;LED灯呈矩形分布而非“8”字形&#xff1b;并且点阵屏和数码管一样&#xff0c;有两种接法共阳极和共阳极&#xff1b; 16*16LED点阵…

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

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

自动驾驶AVM环视算法--相机的联合标定算法实现和exe测试demo

更新&#xff1a;测试的exe程序&#xff0c;无需解压码就可以体验算法测试效果 链接&#xff1a;https://pan.baidu.com/s/1OfuslVNcTXAZWvwiqflWsA 提取码&#xff1a;zoef 1、压缩包解压后显示如下所示 测试文件包括&#xff1a;可执行的exe文件、测试的图片等。 2.双击ex…

二分查找1

1. 二分查找&#xff08;704&#xff09; 题目描述&#xff1a; 算法原理&#xff1a; 暴力解法就是遍历数组来找到相应的元素&#xff0c;使用二分查找的解法就是每次在数组中选定一个元素来将数组划分为两部分&#xff0c;然后因为数组有序&#xff0c;所以通过大小关系舍弃…

大模型lora微调中,rank参数代表什么,怎么选择合适的rank参数

在大模型的LoRA&#xff08;Low-Rank Adaptation&#xff09;微调中&#xff0c;rank参数&#xff08;秩&#xff09;是一个关键的超参数&#xff0c;它决定了微调过程中引入的低秩矩阵的维度。具体来说&#xff0c;rank参数r表示将原始权重矩阵分解成两个低秩矩阵的维度&#…

第一次构建一个对话机器人流程解析(一)

1.问答机器人的组成 1.1 问答机器人的组成结构图 2. 问答机器人的组成-机器人的个人属性 所谓的机器人一般具备有个人的属性&#xff0c;这些属性固定&#xff0c;形成了机器人的个人偏好 在实现过程中&#xff0c;此处使用一个xml配置文件&#xff0c;配置了机器人的个人年…

PHP验证日本免费电话号码格式

首先&#xff0c;您需要了解免费电话号码的格式。 日本免费电话也就那么几个号段&#xff1a;0120、0990、0180、0570、0800等开头的&#xff0c;0800稍微特殊点&#xff0c;在手机号里面有080 开头&#xff0c;但是后面不一样了。 关于免费电话号码的划分&#xff0c;全部写…

大语言模型垂直化训练技术与应用

在人工智能领域&#xff0c;大语言模型&#xff08;Large Language Models, LLMs&#xff09;已经成为推动技术进步的关键力量&#xff0c;垂直化训练技术逐渐成为研究的热点&#xff0c;它使得大模型能够更精准地服务于特定行业和应用场景。本文结合达观数据的分享&#xff0c…

【计算机毕业设计】021基于weixin小程序微信点餐

&#x1f64a;作者简介&#xff1a;拥有多年开发工作经验&#xff0c;分享技术代码帮助学生学习&#xff0c;独立完成自己的项目或者毕业设计。 代码可以私聊博主获取。&#x1f339;赠送计算机毕业设计600个选题excel文件&#xff0c;帮助大学选题。赠送开题报告模板&#xff…