[前端] uniapp地图组件(map)使用与遇到的一些问题总结

1708 0
Honkers 2022-10-21 15:35:30 | 显示全部楼层 |阅读模式
目录

    前言首先先看成品定位图标获取自身经纬度通过经纬度 获取当前城市信息总结


前言

这段时间在开发uniapp的时候使用到map组件
总结一下本次在uniapp中使用map遇到的一些问题
文章分别是基础 定位图标  获取自身经纬度 通过经纬度获取当时城市信息

首先先看成品



废话不多说,直接开始。
首先引入map组件
  1. <template>
  2.     <view class="content">
  3.       <map style="width: 100%; height: 90vh;" :layer-style='5'  :style="{height:mapheight}" :show-location='true'  :latitude="latitude" :longitude="longitude" :markers="marker" :scale="scale"  @markertap="markertap"   @callouttap='callouttap'>
  4.        </map>
  5.     </view>
  6. </template>
  7. <script>
  8.     export default {
  9.         data() {
  10.             return {
  11.                         latitude:23.106574, //纬度
  12.                         longitude:113.324587,//经度
  13.                         scale:13,//缩放级别
  14.                         bottomData:false,
  15.                         marker: [
  16.                                             {
  17.                                             id:0,
  18.                                             latitude: 23.13065,//纬度
  19.                                             longitude: 113.3274,//经度
  20.                                             iconPath: '',    //显示的图标        
  21.                                             rotate:0,   // 旋转度数
  22.                                             width:20,   //宽
  23.                                             height:30,   //高
  24.                                                 //   title:'我在这里',//标注点名
  25.                                             alpha:0.5,   //透明度
  26.                                             callout:{//自定义标记点上方的气泡窗口 点击有效  
  27.                                             content:'天宝大厦',//文本
  28.                                             color:'#ffffff',//文字颜色
  29.                                             fontSize:14,//文本大小
  30.                                             borderRadius:15,//边框圆角
  31.                                             borderWidth:'10',
  32.                                             bgColor:'#e51860',//背景颜色
  33.                                             display:'ALWAYS',//常显
  34.                                             },
  35.                                             },
  36.                                             {
  37.                                                     id:1234597,
  38.                                                     latitude:  23.106574,//纬度
  39.                                                     longitude: 113.324587,//经度
  40.                                                     iconPath: '',    //显示的图标        
  41.                                                     rotate:0,   // 旋转度数
  42.                                                     width:20,   //宽
  43.                                                 height:30,   //高
  44.                                             //  title:'我在这里',//标注点名
  45.                                                     alpha:0.5,   //透明度
  46.                                                 //      label:{//为标记点旁边增加标签   //因背景颜色H5不支持
  47.                                                 //   color:'red',//文本颜色
  48.                                                 //      },
  49.                                                     callout:{//自定义标记点上方的气泡窗口 点击有效  
  50.                                                 content:'广州塔',//文本
  51.                                                 color:'#ffffff',//文字颜色
  52.                                                 fontSize:14,//文本大小
  53.                                                 borderRadius:15,//边框圆角
  54.                                                 borderWidth:'10',
  55.                                                 bgColor:'#e51860',//背景颜色
  56.                                                 display:'ALWAYS',//常显
  57.                                                 },
  58.                                             },
  59.                                             {
  60.                                                     id:2,
  61.                                                     latitude:  23.1338,//纬度
  62.                                                     longitude: 113.33052,//经度
  63.                                                     iconPath: '',    //显示的图标        
  64.                                                     rotate:0,   // 旋转度数
  65.                                                   width:20,   //宽
  66.                                                     height:30,   //高
  67.                                                     alpha:0.5,   //透明度
  68.                                             callout:{//自定义标记点上方的气泡窗口 点击有效  
  69.                                                 content:'德隆大厦',//文本
  70.                                                 color:'#ffffff',//文字颜色
  71.                                                 fontSize:14,//文本大小
  72.                                                 borderRadius:15,//边框圆角
  73.                                                 borderWidth:'10',
  74.                                                 bgColor:'#e51860',//背景颜色
  75.                                                 display:'ALWAYS',//常显
  76.                                                 },
  77.                                             },
  78.                                                 {
  79.                                                     id:3,
  80.                                                     latitude:  23.136455,//纬度
  81.                                                     longitude: 113.329002,//经度
  82.                                                     iconPath: '',    //显示的图标        
  83.                                                     rotate:0,   // 旋转度数
  84.                                                         width:20,   //宽
  85.                                                         height:30,   //高
  86.                                                         alpha:0.5,   //透明度
  87.                                                     callout:{//自定义标记点上方的气泡窗口 点击有效  
  88.                                                     content:'羊城国际商贸中心',//文本
  89.                                                     color:'#ffffff',//文字颜色
  90.                                                     fontSize:14,//文本大小
  91.                                                     borderRadius:15,//边框圆角
  92.                                                     borderWidth:'10',
  93.                                                     bgColor:'#e51860',//背景颜色
  94.                                                     display:'ALWAYS',//常显
  95.                                                     },
  96.                                                 },
  97.                                             {
  98.                                                     id:4,
  99.                                                     latitude:  23.224781,//纬度
  100.                                                     longitude: 113.293911,//经度
  101.                                                     iconPath: '',    //显示的图标        
  102.                                                     rotate:0,   // 旋转度数
  103.                                                   width:20,   //宽
  104.                                                     height:30,   //高
  105.                                                     alpha:0.5,   //透明度
  106.                                                 callout:{//自定义标记点上方的气泡窗口 点击有效  
  107.                                                     content:'天瑞广场A座',//文本
  108.                                                     color:'#ffffff',//文字颜色
  109.                                                     fontSize:16,//文本大小
  110.                                                     borderRadius:15,//边框圆角
  111.                                                     borderWidth:'12',
  112.                                                     bgColor:'#e51860',//背景颜色
  113.                                                     display:'ALWAYS',//常显
  114.                                                 },
  115.                                                 },
  116.                                             {
  117.                                                         id:5,
  118.                                                         latitude:  23.072726,//纬度
  119.                                                         longitude: 113.277921,//经度
  120.                                                         iconPath: '',    //显示的图标        
  121.                                                         rotate:0,   // 旋转度数
  122.                                                 width:20,   //宽
  123.                                                     height:30,   //高
  124.                                                     alpha:0.5,   //透明度
  125.                                             callout:{//自定义标记点上方的气泡窗口 点击有效  
  126.                                                     content:'大米和小米儿童康复(广州盈丰)中心',//文本
  127.                                                     color:'#ffffff',//文字颜色
  128.                                                     fontSize:14,//文本大小
  129.                                                     borderRadius:15,//边框圆角
  130.                                                     borderWidth:'8',
  131.                                                     bgColor:'#e51860',//背景颜色
  132.                                                     display:'ALWAYS',//常显
  133.                                                 },
  134.                                                 },
  135.                                         ],
  136.             }
  137.             },
  138.         onLoad() {
  139.         },
  140.         computed:{
  141.                     mapheight(){
  142.                         let data =''
  143.                         if(this.bottomData){
  144.                             if(this.upTop){
  145.                                 data ='50px'
  146.                             }else{
  147.                                 data ='200px'
  148.                             }
  149.                         }else{
  150.                             data ='90vh'
  151.                         }
  152.                         return data
  153.                     },
  154.                     coverbottom(){
  155.                         let data =''
  156.                         if(this.bottomData){
  157.                             data ='20rpx'
  158.                         }else{
  159.                             data ='100rpx'
  160.                         }
  161.                         return data
  162.                     }
  163.                 },
  164.         methods:{
  165.                 //地图点击事件
  166.             markertap(e) {
  167.                 console.log("===你点击了标记点===",e)
  168.                 },
  169.             //地图点击事件
  170.             callouttap(e){
  171.                 console.log('地图点击事件',e)
  172.         }
  173.         }
  174.     }
  175. </script>
复制代码
得到的样式是这样


这里只给了两个回调 有更多需求可以去uniapp中搜索 map | uni-app官网

定位图标

查了一下官方文档,发现官方文档中并没有关于定位的写法 ,没办法只能自己手写一个了。
大概是这样的。

  1. <template>
  2.                 <view class="page-section page-section-gap map-container">
  3.                                         <map style="width: 100%; height: 90vh;" :layer-style='5'  :style="{height:mapheight}" :show-location='true'  :latitude="latitude" :longitude="longitude" :markers="marker" :scale="scale"  @markertap="markertap"   @callouttap='callouttap'>
  4.                                                 <cover-view class="cover-view" :style='{bottom:coverbottom}' @click="onControltap">
  5.                                                         <cover-image class="cover-image" @click="play" src="@/static/ditudingwei.png"></cover-image>
  6.                                                         <cover-view>
  7.                                                                         定位
  8.                                                         </cover-view>
  9.                                         </cover-view>       
  10.                         </map>
  11.                 </view>
  12. </template>
  13. <script>
  14.         export default {
  15.                 data() {
  16.                         return {
  17.                                                 latitude:23.106574, //纬度
  18.                                                 longitude:113.324587,//经度
  19.                                                 scale:13,//缩放级别
  20.                                                 bottomData:false,
  21.                                                 marker: [
  22.                                                                                         {
  23.                                                                                         id:0,
  24.                                                                                         latitude: 23.13065,//纬度
  25.                                                                                         longitude: 113.3274,//经度
  26.                                                                                         iconPath: '',    //显示的图标        
  27.                                                                                         rotate:0,   // 旋转度数
  28.                                                                                         width:20,   //宽
  29.                                                                                         height:30,   //高
  30.                                                                                                 //   title:'我在这里',//标注点名
  31.                                                                                         alpha:0.5,   //透明度
  32.                                                                                         callout:{//自定义标记点上方的气泡窗口 点击有效  
  33.                                                                                         content:'天宝大厦',//文本
  34.                                                                                         color:'#ffffff',//文字颜色
  35.                                                                                         fontSize:14,//文本大小
  36.                                                                                         borderRadius:15,//边框圆角
  37.                                                                                         borderWidth:'10',
  38.                                                                                         bgColor:'#e51860',//背景颜色
  39.                                                                                         display:'ALWAYS',//常显
  40.                                                                                         },
  41.                                                                                         },
  42.                                                                                         {
  43.                                                                                                         id:1234597,
  44.                                                                                                         latitude:  23.106574,//纬度
  45.                                                                                                         longitude: 113.324587,//经度
  46.                                                                                                         iconPath: '',    //显示的图标        
  47.                                                                                                         rotate:0,   // 旋转度数
  48.                                                                                                         width:20,   //宽
  49.                                                                                                 height:30,   //高
  50.                                                                                         //  title:'我在这里',//标注点名
  51.                                                                                                         alpha:0.5,   //透明度
  52.                                                                                                 //      label:{//为标记点旁边增加标签   //因背景颜色H5不支持
  53.                                                                                                 //   color:'red',//文本颜色
  54.                                                                                                 //      },
  55.                                                                                                         callout:{//自定义标记点上方的气泡窗口 点击有效  
  56.                                                                                                 content:'广州塔',//文本
  57.                                                                                                 color:'#ffffff',//文字颜色
  58.                                                                                                 fontSize:14,//文本大小
  59.                                                                                                 borderRadius:15,//边框圆角
  60.                                                                                                 borderWidth:'10',
  61.                                                                                                 bgColor:'#e51860',//背景颜色
  62.                                                                                                 display:'ALWAYS',//常显
  63.                                                                                                 },
  64.                                                                                         },
  65.                                                                                         {
  66.                                                                                                         id:2,
  67.                                                                                                         latitude:  23.1338,//纬度
  68.                                                                                                         longitude: 113.33052,//经度
  69.                                                                                                         iconPath: '',    //显示的图标        
  70.                                                                                                         rotate:0,   // 旋转度数
  71.                                                                                                   width:20,   //宽
  72.                                                                                                         height:30,   //高
  73.                                                                                                         alpha:0.5,   //透明度
  74.                                                                                         callout:{//自定义标记点上方的气泡窗口 点击有效  
  75.                                                                                                 content:'德隆大厦',//文本
  76.                                                                                                 color:'#ffffff',//文字颜色
  77.                                                                                                 fontSize:14,//文本大小
  78.                                                                                                 borderRadius:15,//边框圆角
  79.                                                                                                 borderWidth:'10',
  80.                                                                                                 bgColor:'#e51860',//背景颜色
  81.                                                                                                 display:'ALWAYS',//常显
  82.                                                                                                 },
  83.                                                                                         },
  84.                                                                                                 {
  85.                                                                                                         id:3,
  86.                                                                                                         latitude:  23.136455,//纬度
  87.                                                                                                         longitude: 113.329002,//经度
  88.                                                                                                         iconPath: '',    //显示的图标        
  89.                                                                                                         rotate:0,   // 旋转度数
  90.                                                                                                                 width:20,   //宽
  91.                                                                                                                 height:30,   //高
  92.                                                                                                                 alpha:0.5,   //透明度
  93.                                                                                                         callout:{//自定义标记点上方的气泡窗口 点击有效  
  94.                                                                                                         content:'羊城国际商贸中心',//文本
  95.                                                                                                         color:'#ffffff',//文字颜色
  96.                                                                                                         fontSize:14,//文本大小
  97.                                                                                                         borderRadius:15,//边框圆角
  98.                                                                                                         borderWidth:'10',
  99.                                                                                                         bgColor:'#e51860',//背景颜色
  100.                                                                                                         display:'ALWAYS',//常显
  101.                                                                                                         },
  102.                                                                                                 },
  103.                                                                                         {
  104.                                                                                                         id:4,
  105.                                                                                                         latitude:  23.224781,//纬度
  106.                                                                                                         longitude: 113.293911,//经度
  107.                                                                                                         iconPath: '',    //显示的图标        
  108.                                                                                                         rotate:0,   // 旋转度数
  109.                                                                                                   width:20,   //宽
  110.                                                                                                         height:30,   //高
  111.                                                                                                         alpha:0.5,   //透明度
  112.                                                                                                 callout:{//自定义标记点上方的气泡窗口 点击有效  
  113.                                                                                                         content:'天瑞广场A座',//文本
  114.                                                                                                         color:'#ffffff',//文字颜色
  115.                                                                                                         fontSize:16,//文本大小
  116.                                                                                                         borderRadius:15,//边框圆角
  117.                                                                                                         borderWidth:'12',
  118.                                                                                                         bgColor:'#e51860',//背景颜色
  119.                                                                                                         display:'ALWAYS',//常显
  120.                                                                                                 },
  121.                                                                                                 },
  122.                                                                                         {
  123.                                                                                                                 id:5,
  124.                                                                                                                 latitude:  23.072726,//纬度
  125.                                                                                                                 longitude: 113.277921,//经度
  126.                                                                                                                 iconPath: '',    //显示的图标        
  127.                                                                                                                 rotate:0,   // 旋转度数
  128.                                                                                                 width:20,   //宽
  129.                                                                                                         height:30,   //高
  130.                                                                                                         alpha:0.5,   //透明度
  131.                                                                                         callout:{//自定义标记点上方的气泡窗口 点击有效  
  132.                                                                                                         content:'大米和小米儿童康复(广州盈丰)中心',//文本
  133.                                                                                                         color:'#ffffff',//文字颜色
  134.                                                                                                         fontSize:14,//文本大小
  135.                                                                                                         borderRadius:15,//边框圆角
  136.                                                                                                         borderWidth:'8',
  137.                                                                                                         bgColor:'#e51860',//背景颜色
  138.                                                                                                         display:'ALWAYS',//常显
  139.                                                                                                 },
  140.                                                                                                 },
  141.                                                                                 ],
  142.                         }
  143.                         },
  144.                 onLoad() {
  145.                 },
  146.                 computed:{
  147.                                         mapheight(){
  148.                                                 let data =''
  149.                                                 if(this.bottomData){
  150.                                                         if(this.upTop){
  151.                                                                 data ='50px'
  152.                                                         }else{
  153.                                                                 data ='200px'
  154.                                                         }
  155.                                                 }else{
  156.                                                         data ='90vh'
  157.                                                 }
  158.                                                 return data
  159.                                         },
  160.                                         coverbottom(){
  161.                                                 let data =''
  162.                                                 if(this.bottomData){
  163.                                                         data ='20rpx'
  164.                                                 }else{
  165.                                                         data ='100rpx'
  166.                                                 }
  167.                                                 return data
  168.                                         }
  169.                                 },
  170.                 methods:{
  171.                                 //地图点击事件
  172.                         markertap(e) {
  173.                                 console.log("===你点击了标记点===",e)
  174.                                 },
  175.                         //地图点击事件
  176.                         callouttap(e){
  177.                                 console.log('地图点击事件',e)
  178.                 }
  179.                 }
  180.         }
  181. </script>
  182. <style lang='less' scoped>
  183.                 .map-container {
  184.                 position: relative;
  185.                 overflow: hidden;
  186.                 .cover-view {
  187.                         display: flex;
  188.                         flex-direction: column;
  189.                         align-items: center;
  190.                         justify-content: center;
  191.                         width: 80rpx;
  192.                         height: 80rpx;
  193.                         color: #484848;
  194.                         background-color: #fff;
  195.                         background-size: 120rpx 120rpx;
  196.                         background-position: center center;
  197.                         position: absolute;
  198.                         bottom: 100rpx;
  199.                         right: 32rpx;
  200.                 }
  201.                 .cover-image{
  202.                         display: inline-block;
  203.                         width: 30rpx;
  204.                         height: 30rpx;
  205.                 }
  206.                 }
  207. </style>
复制代码
获取自身经纬度

进入页面拿到用户自身的经纬度
在onShow中输入
  1. onShow() {
  2.                                 uni.getLocation({
  3.                                                                 geocode:true,
  4.                                                                 type: 'wgs84',
  5.                                                                 success: (res) => {
  6.                                                                         console.log(res,'获取当前位置')
  7.                                                                 },
  8.                                                                 address:(res) =>{
  9.                                                                         console.log('address',res)
  10.                                                                 }
  11.                                 });
复制代码


需要打开manifest.json


如果从来没配置过appld请先配置在进行操作




然后重新编译就会出现


允许之后就可以拿到经纬度信息


然后可以将经纬度信息赋值给data中的latitude longitude
  1.         onShow() {
  2.                                 uni.getLocation({
  3.                                                                 geocode:true,
  4.                                                                 type: 'wgs84',
  5.                                                                 success: (res) => {
  6.                                                                         console.log(res,'获取当前位置')
  7.                                                                         this.longitude =res.longitude
  8.                                                                         this.latitude = res.latitude
  9.                                                                 },
  10.                                                                 address:(res) =>{
  11.                                                                         console.log('address',res)
  12.                                                                 }
  13.                                 });
  14.                 },
复制代码
通过经纬度 获取当前城市信息

首先需要进入腾讯位置服务 /控制台/应用管理/我的应用/添加key选择webServiceApi


然后去小程序开发管理/开发设置/服务器域名/request合法域名后面新增
https://apis.map.qq.com;
然后下载
https://mapapi.qq.com/web/miniprogram/JSSDK/qqmap-wx-jssdk1.1.zip
引入

在ohshow中使用
  1. var qqmapsdk
  2.                         // 实例化API核心类
  3.                         qqmapsdk = new QQMapWX({
  4.                                 key: '创建的key'
  5.                         });
  6.                         qqmapsdk.reverseGeocoder({
  7.                         location:{
  8.                                 latitude:this.latitude,
  9.                                 longitude:this.longitude
  10.                         },
  11.                         success:(res)=>{
  12.                                 console.log('reverseGeocoder',res)
  13.                         },
  14.                         fail:(err)=>{
  15.                                 console.log('reverseGeocoder',err)
  16.                         }
  17.                 })
复制代码
这样就可以通过自身的经纬度获取到当前所在的详细信息
更多地图事件可以查询官网腾讯位置服务 - 立足生态,连接未来
关于地图气泡弹窗在真机不显示或者显示颜色不明显的情况,你可以使用在callout中使用padding
这样就可以显示颜色了
在uniapp中使用地图大概就这些

总结

到此这篇关于uniapp地图组件(map)使用与遇到的一些问题总结的文章就介绍到这了,更多相关uniapp地图组件(map)使用内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章希望大家以后多多支持中国红客联盟!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

特级红客

关注
  • 3159
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行