当前位置: 首页 > news >正文

vue中使用腾讯API地图,实现搜索关键词获取经纬度和地址并显示地图

vue中使用腾讯API地图,实现搜索关键词获取经纬度和地址并显示地图

  1. 调用腾讯地图api前需要先去注册并申请key
  2. 在vue项目中的/public文件夹中的index.html的head中写入
  3. 安装lodash:npm install --save lodash
  4. 安装jsonp: npm install vue-jsonp --save
  5. main.js引入jsonp
importVuefrom'vue'import{VueJsonp}from'vue-jsonp'Vue.use(VueJsonp)Vue.config.productionTip=falsenewVue({router,store,render:h=>h(App)}).$mount('#app')

在public中的index.html文件中引入script标签加上链接

<script src="https://map.qq.com/api/gljs?v=1.exp&key=OB4BZ***"></script>// key 换成自己的key

具体代码功能如下:

// template中<template><div id="app"><divclass="box"><divclass="search-box"><input type="text"v-model="searchValue"class="search"@input="searchHandler"><ulclass=""v-if="kwData.length != 0"><li v-for="(kw, i) in kwData":key="i"@click="clickLiHandler(kw)">{{kw.title}}</li></ul></div><h3>地址:{{address}}</h3><divclass="location"><h3>经度:{{lng}}</h3><h3>纬度:{{lat}}</h3></div><br/><div id='mapContainer'></div></div></div></template>// script<script>// import { debounce } from 'throttle-debounce'// 安装lodash:npm install lodash --save 安装jsonp: npm install vue-jsonp --saveimportlodashfrom'lodash'letthatexportdefault({data(){return{addValue:'',longitude:'',latitude:'',searchValue:'',kwData:'',clickLi:true,lng:'',lat:'',address:'',markerUrl:require("./assets/logo.png"),// 点标记图片路径map:'',zoom:4,// 地图一开始的缩放级别center:newwindow.TMap.LatLng(39.984120,116.307484),// 地图一开始的中心点markerLayer:'',geometries:[]}},created(){that=this},mounted(){this.init1()},methods:{init1(){vardrawContainer=document.getElementById('mapContainer');// var center = new window.TMap.LatLng(39.984104, 116.307503);//设置中心点坐标 lat: 23.228237 lng: 113.273747varcenter=newwindow.TMap.LatLng(23.228237,113.273747);//设置中心点坐标 lat: 23.228237 lng: 113.273747this.map=newwindow.TMap.Map(drawContainer,{zoom:9,pitch:40,center:center,draggable:true,scrollable:true,mapStyleId:"style 1"});console.log('drawContainer',drawContainer)console.log(111,this.map)// 创建点聚合实例varmarkerCluster=newwindow.TMap.MarkerCluster({id:'cluster',map:this.map,enableDefaultStyle:true,// 启用默认样式minimumClusterSize:1,// 形成聚合簇的最小个数geometries:this.geometries,zoomOnClick:true,// 点击簇时放大至簇内点分离gridSize:60,// 聚合算法的可聚合距离averageCenter:false,// 每个聚和簇的中心是否应该是聚类中所有标记的平均值maxZoom:10// 采用聚合策略的最大缩放级别})markerCluster},// 添加点标记addPoint(point){// 一个point需要有id和坐标this.markerLayer.add([{'id':point['id'],'styleId':'mystyle',"position":newwindow.TMap.LatLng(point['latitude'],point['longitude']),"properties":{}// 自定义属性,可以没有}])},addrToGetCoordinate(event){console.log('event',event)console.log(event.target.value)letaddr=event.target.valuethis.$jsonp('https://apis.map.qq.com/ws/geocoder/v1/',{key:'6V3BZ-****',// 换成自己的keyoutput:'jsonp',address:addr}).then((res)=>{console.log(res)if(res.status===0){// 处理得到的经纬度this.longitude=res.result.location.lngthis.latitude=res.result.location.lat console.log('经度',this.longitude)console.log('纬度',this.latitude)// 用获取到的经纬度,修改地图的中心点// this.changeCenter(res.result.location.lat.toFixed(6), res.result.location.lng.toFixed(6))}}).catch((e)=>{console.log(e)})},getKwd(event){letkw=event.target.value console.log(111,kw)this.$jsonp('https://apis.map.qq.com/ws/place/v1/suggestion',{key:'6V3BZ****',// 换成自己的keyoutput:'jsonp',keyword:kw,region:'广州市'}).then((res)=>{console.log(res)if(res.status===0){console.log(res)this.kwData=res.data// 用获取到的经纬度,修改地图的中心点// this.changeCenter(res.result.location.lat.toFixed(6), res.result.location.lng.toFixed(6))}else{this.kwData=[]}}).catch((e)=>{console.log(e)})},searchHandler:lodash.debounce((event)=>{console.log(222,this)console.log('event',event)that.getKwd(event)},500),clickLiHandler(kwObj){// 点击搜索关键词项// this.clickLi = falsethis.searchValue=kwObj.titlethis.lng=kwObj.location.lngthis.lat=kwObj.location.latthis.address=kwObj.addressthis.geometries=[{// 点数组position:newwindow.TMap.LatLng(this.lat,this.lng)}]this.init1()console.log(this.lat,this.lng)this.kwData=[]}}})</script><style lang="less">#app{font-family:Avenir,Helvetica,Arial,sans-serif;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;text-align:center;color:#2c3e50;.search{margin-top:20px;}.box{margin:0auto;width:1080px;margin-top:80px;input{outline:none;width:300px;height:30px;border:1px solid #ccc;padding:10px;background-color:white;box-sizing:border-box;}.search-box{position:relative;ul{// border: 1px solid #ccc;// box-shadow: 1px, 1px, 1px, 1px, #ccc;box-shadow:0px 5px 15pxrgba(165,165,165,0.438);width:300px;position:absolute;left:389px;bottom:-293px;box-sizing:border-box;padding:10px;font-size:12px;z-index:100;background-color:white;li{list-style:none;padding:5px;text-align:left;&:hover{background-color:#7ad9ff;cursor:pointer;}}}}.location{display:flex;text-align:center;justify-content:center;h2{width:200px;&::nth-child(1){margin-right:20px;}}}}.map{width:700px;height:700px;}#mapContainer{width:600px;height:600px;margin:0auto;}}#nav{padding:30px;a{font-weight:bold;color:#2c3e50;&.router-link-exact-active{color:#42b983;}}}</style>

效果图

查考:https://blog.csdn.net/l13620804253/article/details/117259244

http://www.jsqmd.com/news/1387381/

相关文章:

  • 池州市石台县OEM白标贴牌GEO服务商怎么选?2026年靠谱推荐与选择指南 - 科技快讯
  • 从零开始的单元测试:csharp_with_csharpfritz教你构建可靠C代码
  • 2026年合肥市包河区GEO服务商代理加盟本地靠谱推荐:城市合伙人模式解析 - 企业新闻快传
  • lamp-boot表单校验前后端统一验证:提升数据安全性的关键步骤
  • 济南网站建设工作室如何选择靠谱团队避坑指南与实战经验全解析
  • Windows微信QQ防撤回神器:3分钟永久保留重要消息
  • 搞笑视频瞬间变表情包!2026免费GIF制作完整攻略,两种主流工具保姆级教程 - 时时资讯
  • 【路径规划】基于遗传算法结合粒子群算法求解TSP问题matlab代码
  • 铜陵市枞阳县OEM白标贴牌GEO服务商怎么选?2026年靠谱推荐与判断标准 - 企业新闻快传
  • 30分钟完成黑苹果配置:OpCore-Simplify终极指南让OpenCore EFI创建变得简单快速
  • 蓝队实战笔记:一次 BOLA 越权攻击的防守复盘——从告警发现到溯源加固
  • 不为人知的开源硬件控制工具:告别后台弹窗,把游戏本还给自己
  • 朋友圈九宫格发图总错位、对不齐?2026两套免费方案|一键分割,像素级精准不翻车 - 时时资讯
  • AI论文网站最全盘点:从语法纠错到查重降AI,这一篇承包你的全部痛点 - 论文助教
  • 如何快速掌握Hap QuickTime Codec:专业视频编码终极教程
  • 东莞网站建设多少钱,资深老哥掏心窝子告诉你底价与避坑指南
  • 滁州市来安县OEM白标贴牌GEO服务商怎么选?2026年靠谱推荐与避坑指南 - 小随科技
  • Docker部署Garage-WebUI教程:简单高效的分布式存储解决方案
  • 鸣潮自动化工具ok-ww完整指南:解放双手的智能游戏助手
  • 花都网站建设公司哪家强?揭秘本地专业团队如何帮企业打造高转化官网
  • 上海html5网站建设如何实现企业品牌价值的最大化提升
  • MobaXterm中文版:一体化远程管理的技术革新方案
  • r8brain-free-src常见问题解答:解决音频重采样中的99%难题
  • 大麦网抢票脚本逆向工程:从零构建自动化购票系统的完整指南
  • 吉安市网站建设:从本地中小企业的数字化转型看网站优化的未来走向
  • 界面控件DevExpress Blazor UI v22.2 - 折叠组件、数据编辑器升级增强
  • 2026金华婺城区楼顶漏水避坑指南,本地老牌公司,质保可查 - 专业防水施工
  • 深度揭秘天津市建设行业联合会网站:探索天津建筑行业资源整合与数字化转型的新高地
  • Embabel Agent Framework实战:构建星座新闻发现器的完整教程
  • 2026杭州桐庐县楼顶漏水避坑指南,本地老牌公司,质保可查 - 专业防水施工