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.pswp.cn/web/39965.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/39965.shtml
英文地址,請注明出處:http://en.pswp.cn/web/39965.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

SS8812T替代DRV8812的國產雙通道H橋電機驅動芯片

由工采網代理的SS8812T是一款國產雙通道H橋電機驅動芯片&#xff1b;該芯片為打印機和其它電機一體化應用提供一種雙通道集成電機驅動方案&#xff1b;可Pin-to-Pin兼容替代DRV8812&#xff0c;可廣泛應用于POS、打印機、安防相機、辦公自動化設備、游戲機、機器人等。 產品描述…

Vue.js 案例——商品管理

一.需要做出的效果圖&#xff1a; 二.實現的步驟 首先&#xff0c;先建一個項目&#xff0c;命名Table&#xff0c;在Table項目中的components里新建一個MyTable.vue文件。 第二步&#xff0c;在原有的 HelloWorld.vue中寫入代碼。 HelloWorld.vue代碼如下&#xff1a; <…

KumiaoQQ機器人框架源碼

源碼介紹 酷喵機器人框架基于PC協議與MGCH的結合&#xff0c;MGCH即 MiraiGO-CQhttp&#xff08;代碼類型&#xff1a;易語言&#xff09;基本的API功能已經實現&#xff0c;具體可自測&#xff08;教程/日志/說明文本已附帶&#xff09;開放源碼僅供參考學習交流&#xff0c;…

遠超美國!中國AI專利數量全球第一!商湯推出面向C端用戶大模型“Vimi”,可生成分鐘級視頻!|AI日報

文章推薦 蘋果獲得OpenAI董事會觀察員職位&#xff01;Runway正籌集新一輪融資&#xff0c;估值40億美元&#xff01;&#xff5c;AI日報 AI基準測評&#xff08;下&#xff09;&#xff1a;視頻生成、代碼能力、邏輯推理&#xff0c;AI是否已經超越人類&#xff1f; 聯合國…

【linux高級IO(一)】理解五種IO模型

&#x1f493;博主CSDN主頁:杭電碼農-NEO&#x1f493; ? ?專欄分類:Linux從入門到精通? ? &#x1f69a;代碼倉庫:NEO的學習日記&#x1f69a; ? &#x1f339;關注我&#x1faf5;帶你學更多操作系統知識 ? &#x1f51d;&#x1f51d; Linux高級IO 1. 前言2. 重談對…

kubernetes dashboard安裝

1.查看符合自己版本的kubernetes Dashboard 比如我使用的是1.23.0版本 https://github.com/kubernetes/dashboard/releases?page5 對應版本 kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.5.1/aio/deploy/recommended.yaml修改對應的yaml,…

Linux Conda 入門案例教程

Conda 的基本概念 1.什么是 Conda&#xff1f;&#xff1a;Conda 是一個開源的包管理器和環境管理器&#xff0c;用于管理 Python 和其他語言的環境和依賴項。 2.Conda 的特點&#xff1a;Conda 的特點包括快速、可靠、靈活和跨平臺支持等。 安裝和配置 1.安裝 Conda&#x…

adb不插usb線通過wifi調試

說起做手機開發也有好多年了&#xff0c;說來慚愧&#xff0c;我最近才知道安卓手機是可以不插數據線進行開發調試的。起因是公司近期采購了一批安卓一卡通設備&#xff0c;需要對其進行定制開發APP,但是由于我插USB調試發現沒有反應。通過詢問廠家才知道可以通過WIFI進行調試。…

請注意,以下這幾種操作都會導致流量卡被停用!

最近一段時間&#xff0c;小編經常收到一些反饋&#xff0c;明明是剛辦理的手機號還沒有用幾天就被停用了&#xff0c;今天&#xff0c;這篇文章我們要了解就是手機號被停用的問題。 ? 對于新辦理的手機號會被停用這個問題&#xff0c;主要還是因為運營商為了防止電話詐騙&…

vue監聽數據時 newValue, oldValue操作處理

要只存入新更改的數據&#xff0c;可以在 watch 的回調函數中進行比較&#xff0c;篩選出有變化的屬性并將其存入新數組。以下是一個示例代碼&#xff0c;假設要監聽的對象為 obj&#xff1a; data() {return {differenceArray: [], obj: { /* 對象的初始屬性 */ }}; }, compu…

java數據結構集合復習之包裝類和泛型

前言: 這是我最一年學習java的一部分的回顧總結 1.包裝類 在Java中&#xff0c;由于基本類型不是繼承自Object&#xff0c;為了在泛型代碼中可以支持基本類型&#xff0c;Java給每個基本類型都對應了一個包裝類型。 1.1基本數據類型和對應的包裝類 ----—基本數據類型包裝類…

ubuntu軟件源的兩種格式和環境變量

1. ubuntu的/etc是什么目錄&#xff1f; 在Ubuntu操作系統中&#xff0c;/etc/是一個特殊的目錄&#xff0c;它包含系統的配置文件。這些配置文件用于設置各種系統和應用程序的參數和選項。 一般來說&#xff0c;用戶可以在這個目錄下找到各種重要的配置文件&#xff0c;如網絡…

Web3 ETF的主要功能

Web3 ETF的主要功能可以概括為以下幾點&#xff0c;Web3 ETF仍是一項新興投資產品&#xff0c;其長期表現仍存在不確定性。投資者在投資Web3 ETF之前應仔細研究相關風險&#xff0c;并做好充分的風險評估。北京木奇移動技術有限公司&#xff0c;專業的軟件外包開發公司&#xf…

商務辦公優選!AOC Q27E3S2商用顯示器,打造卓越新體驗!

摘要&#xff1a;助辦公室一族縱橫職場&#xff0c;實現高效舒適辦公&#xff01; 在日常商務辦公中&#xff0c;對于辦公室一族來說總有太多“難難難難難點”&#xff1a;工作任務繁瑣&#xff0c;熬夜加班心力交瘁、長時間伏案工作導致頸椎、眼睛等出現問題&#xff0c;職業…

BBA車主,千萬別去試駕問界M9

文 | AUTO芯球 作者 | 雷慢&響鈴 我勸你啊&#xff0c;千萬別去試駕問界M9&#xff0c; 不然啊&#xff0c;可能1個小時50萬就沒了&#xff0c; 不信你看這個“大冤種”&#xff0c; 他曾經發誓打死不買電車&#xff0c; 考慮了三、四年換寶馬X5&#xff0c; 結果談完…

GNU/Linux - 如何編譯kernel

使用 make 命令構建 Linux 內核涉及多個步驟。下面是整個過程的基本概述&#xff1a; 1. 獲取內核源代碼 * 從 kernel.org 或你的發行版軟件倉庫下載內核源代碼。 * 將源代碼解壓縮到一個目錄中。 2. 配置內核 * 切換到內核源代碼目錄。 * 可選擇清理源代碼樹&#xff1a;mak…

前端面試題5(前端常見的加密方式)

前端常見的加密方式 在前端進行數據加密主要是為了保護用戶的隱私和提升數據傳輸的安全性。前端數據加密可以采用多種方法&#xff0c;以下是一些常見的加密技術和方法&#xff1a; 1. HTTPS 雖然不是直接的前端加密技術&#xff0c;但HTTPS是保障前端與后端數據傳輸安全的基…

關于MCU-Cortex M7的存儲結構(flash與SRAM)

MCU并沒有DDR&#xff0c;所以他把代碼存儲在flash上&#xff0c;臨時變量和棧運行在SRAM上。之所以這么做是因為MCU的cpu頻率很低&#xff0c;都是幾十MHZ到一二百MHZ&#xff0c;flash的讀取速度能夠滿足cpu 的取指需求&#xff0c;但flash 的寫入速度很慢&#xff0c;所以引…

剛辦理的手機號被停用,你可能遇到這些問題了!

很多朋友都會遇到手機號被停用的情況&#xff0c;那么你知道你的手機號為什么會被停用嗎&#xff1f;接下來&#xff0c;關于手機號被停用的問題&#xff0c;跟著小編一塊來了解一下吧。 ?停機的兩種形態&#xff1a; 1、第一個是局方停機&#xff0c;即語音、短信和流量都不…

xmind2testcase工具將測試用例從Xmind轉為CSV導入禪道

使用xmind編寫測試用例&#xff0c;使用xmind2testcase工具將測試用例從Xmind轉為CSV導入禪道&#xff0c;便于管理。 1.工具準備 第一步&#xff1a;安裝python 第二步&#xff1a;安裝xmind2testcase工具 運行-cmd-打開命令提示符彈窗&#xff0c;輸入安裝命令 安裝命令&…