vue项目中使用腾讯地图,gl版本,qq地图

news/2024/11/23 23:28:20/

参考链接

https://blog.csdn.net/weixin_30552635/article/details/95095724

介绍

在vue项目中使用腾讯地图,我这里使用了gl版本,它呈现的效果更好,与老的2d版本相比它们的API大致相同。
使用方法,ctrl c + ctrl v。

使用效果

在这里插入图片描述

组件代码

<!-- 腾讯地图pcweb -xwjs -->
<template><div><div class="search-con"><el-input size="mini" v-model="CURR_ADR"></el-input><el-button @click="search" style="margin-left:20px" type="primary" size="mini">查询</el-button></div><div :id="elId" class="tmap"></div></div>
</template>
<script>
export default {props: {elId: {type: String,require: true,},},data() {return {CURR_MAP: [null, null], //当前坐标CURR_ADR: null, //当前地址KEY: "NJKBZ-FS2CS-6C6O4-6WWYM-4NQ63-MJBNO",APP_NAME: "像铸",map: null, //地图对象Maker: null, //标记(单个)}},mounted() {this.initMap()},beforeDestroy() {// this.map.off("click")},methods: {createScript() {return new Promise((resolveOut) => {const scripts = document.getElementsByTagName("script")const addScripts = Array.from(scripts)// 防止重复创建scriptconst findxzmap = addScripts.find((el) => el.id == "xzmap")const findxzgeo = addScripts.find((el) => el.id == "xzgeo")const findjq = addScripts.find((el) => el.id == "jquery")const promistList = []if (!findjq) {const p1 = new Promise((resolve) => {const sc = document.createElement("script")sc.src = `https://code.jquery.com/jquery-2.2.4.js`sc.setAttribute("id", "jquery")sc.type = "text/javascript"document.body.appendChild(sc)sc.onload = () => {resolve()}})promistList.push(p1)}// tmapglif (!findxzmap) {const p2 = new Promise((resolve) => {const sc = document.createElement("script")sc.src = `https://map.qq.com/api/gljs?v=1.exp&key=${this.KEY}`sc.setAttribute("id", "xzmap")sc.type = "text/javascript"document.body.appendChild(sc)sc.onload = () => {resolve()}})promistList.push(p2)}// 精准定位apiif (!findxzgeo) {const p3 = new Promise((resolve) => {const scgeo = document.createElement("script")scgeo.src ="https://mapapi.qq.com/web/mapComponents/geoLocation/v/geolocation.min.js"scgeo.setAttribute("id", "xzgeo")scgeo.type = "text/javascript"document.body.appendChild(scgeo)scgeo.onload = () => {resolve()}})promistList.push(p3)}Promise.all(promistList).then(() => {const { TMap, qq } = windowresolveOut({ TMap, qq })})})},async initMap() {const { TMap, qq } = await this.createScript()this.TMap = TMapthis.qq = qqconst geolocation = new this.qq.maps.Geolocation(this.KEY, this.APP_NAME)geolocation.getLocation((val) => {const { lat, lng } = valthis.CURR_MAP = [lat, lng]this.init()})},AddMark() {const center = new this.TMap.LatLng(this.CURR_MAP[0], this.CURR_MAP[1])if (this.Maker) {this.Maker.setMap(null)}this.Maker = new this.TMap.MultiMarker({id: "a-marker",map: this.map,geometries: [{position: center,},],})},RefMap() {const center = new this.TMap.LatLng(this.CURR_MAP[0], this.CURR_MAP[1])this.map.panTo(center)},search() {if (!this.CURR_ADR) {this.$message.warning('查询数据不能为空')return}this.getAddr('search')},getAddr(type) {const url = "http://apis.map.qq.com/ws/geocoder/v1/"const data = {address: type === 'search' ? this.CURR_ADR : undefined,location: type === 'search' ? undefined : this.CURR_MAP[0] + "," + this.CURR_MAP[1],get_poi: type === 'search' ? undefined : 0,key: this.KEY,output: 'jsonp'}$.ajax({type: "get",dataType: "jsonp",data: data,jsonp: "callback",jsonpCallback: "QQmap",url: url,success: (res) => {// console.log(res)if (res && res.status == 0 && res.result.location) {// 正向搜索let adddr = ''if (res.request_id) {adddr = `${res.result.address_component.province}${res.result.address_component.city}${res.result.formatted_addresses.recommend}`this.CURR_ADR = adddr} this.CURR_MAP = [res.result.location.lat, res.result.location.lng]this.$emit('mapSearch', { addr: adddr, latLng:  this.CURR_MAP})//刷新坐标轴this.RefMap()//刷新makerthis.AddMark()} else {this.$message.warning('没有搜索到')}},error: () => {this.$message.error('服务错误,请刷新浏览器后重试')},})},init() {//定义map变量this.map = new this.TMap.Map(document.getElementById(this.elId), {center: new this.TMap.LatLng(this.CURR_MAP[0], this.CURR_MAP[1]),zoom: 10,})this.getAddr()//添加监听事件this.map.on("click", (e) => {this.CURR_MAP = [e.latLng.getLat().toFixed(5),e.latLng.getLng().toFixed(5),]this.getAddr()})},},
}</script>
<style lang="scss" scoped>
.search-con {display: flex;align-items: center;
}
.tmap {margin: 20px 0;border: 1px solid #ddd;
}
</style>

父组件调用

<qqMap elId="addr" @mapSearch="mapSearch"/>

http://www.ppmy.cn/news/168630.html

相关文章

Mapboxgl + KrigingJS 实现插值图

&#xff08;1&#xff09;主要过程&#xff1a;结合Kriging.js 实现 Mapboxgl 上的插值图 &#xff08;2&#xff09;效果&#xff1a; &#xff08;3&#xff09;代码 HTML 、CSS&#xff1a; <head><meta charset"UTF-8" /><title>Mapboxgl(…

echarts-gl + echarts + vue2实现3D饼图

参考&#xff1a;参考地址 echarts引入在这就不讲了 一、安装&#xff1a; npm install echarts-gl --save二、局部引入&#xff1a;在需要使用的文件引入即可 import echarts-gl;三、完整代码 <template><div ref"Dpie" id"Dpie" class&quo…

使用echarts-gl 绘制3D地球配置详解

大屏可视化绘制关联配置绘制3D地球 为 ECharts 准备一个定义了宽高的 DOM <div :id"container attrs.id" :style"width: (attrs.width) px; height: (attrs.height) px; z-index:1;"> </div> 实例化 //初始化地图async initEcharts() …

mapbox-gl开发教程(十四):画圆技巧

mapbox-gl提供了一个画圆的图层方式&#xff0c;图层的类型设置成circle&#xff0c;通过设置circle-radius&#xff0c;实现圆的大小变化&#xff0c;不过这个参数的单位是像素&#xff08;pixels&#xff09;&#xff0c;在地图缩放的时候&#xff0c;圆一直是像素尺寸&#…

mapboxGL轨迹展示与播放

概述 历史轨迹回放是GIS很常见的一个功能&#xff0c;本文结合turf.js实现轨迹的展示与播放动画。 效果 实现功能 轨迹的展示&#xff1b;轨迹的方向的箭头展示&#xff1b;随着轨迹播放的小车&#xff0c;并调整方向与轨迹方向一致&#xff1b;已播放路径的展示&#xff1b…

app闪退之-- GL error: Out of memory!

客户应用运行一段时间后闪退: 日志 2021-09-16 10:35:16.194 2328-2328/? I/Choreographer: Skipped 55 frames! The application may be doing too much work on its main thread. 2021-09-16 10:35:16.207 223-394/? E/BufferQueueProducer: [Toast] dequeueBuffer: Buf…

OpenGL 太阳系行星拾取例子(GL_SELECT) VS2008 + glut实现

太阳系&#xff1a;Solar System 以太阳&#xff08;Sun&#xff09;为中心&#xff0c;由内到外分别是&#xff1a; 水星&#xff08;Mercury&#xff09; 金星&#xff08;Venus&#xff09; 地球&#xff08;Earth&#xff09; 火星&#xff08;Mars&#xff09; 木星&#…

android 内存泄露-抓出重要函数-GL_OUT_OF_MEMORY-GL error: Out of memory!OpenGLRenderer

一般log有错误的内存泄露提示“GL error: Out of memory!”"GL_OUT_OF_MEMORY",我们就需要使用工具去一步一步的获取哪些模块类里面的方法出了问题,然后一个一个去尝试找出问题,以下是个人经历: 问题点:蓝牙传输多个文件,引发蓝牙报停,log打印crash: OpenGL…