小程序自定义marker弹出框教程

news2024/11/26 4:39:39

需求背景

微信小程序开发,需要使用腾讯地图显示自定义marker,并且点击marker后弹出自定义的customCallout,并且customCallout的内容为用户点击marker的时候再从后台接口获取数据。

百度了一圈后发现居然没有一篇文章可以一次性完成,可悲。

效果图如下

教程

这里使用的是【微信开发者工具】,不是uniapp。

index.wxml

<map id="myMap" style="width: 100%; height: 200px;" 
        latitude="{{locationObj.latitude}}" 
        longitude="{{locationObj.longitude}}" 
        markers="{{markers}}" 
        bindtap="clickMapFun"
        bindcallouttap="callouttapFun" 
        bindmarkertap="markertapFun">
        <block wx:for="{{markers}}" wx:key="id">
          <cover-view wx:if="{{showInfoBox==true}}" slot="callout">
            <cover-view class="customCallout" marker-id="{{item.id}}">
              <cover-view class="calloutCustomTitle">设备信息</cover-view>
              <cover-view>所属公司:{{item.customObj.companyName}}</cover-view>
              <cover-view>设备标识:{{item.customObj.devId}}</cover-view>
              <cover-view>设备名称:{{item.customObj.name}}</cover-view>
              <cover-view>在线状态:{{item.customObj.onlineStatus}}</cover-view>
              <cover-view>设备状态:{{item.customObj.deviceStatus}}</cover-view>
            </cover-view>
          </cover-view>
        </block>
      </map>

index.js

 

// index.js
// 获取应用实例
const app = getApp()
Page({
  data: {
    showInfoBox: false,
    locationObj: {
      longitude: 121.32406,
      latitude: 31.326717
    },
    markers: [],
    queryParams: {
      id: 0,
      name: ""
    },
    
  },

  clickMapFun(){
    // 用户点击了地图,隐藏自定义弹出框
    this.setData({
      showInfoBox:false
    });
  },
  callouttapFun() {

  },
  markertapFun(e) {
    // 开发的时候,会报错,错误消息如下
//    Do not have markertapFun handler in component: pages/index/index. Please make sure that markertapFun handler has been defined in pages/index/index.
// 报错就点击再次编译,如果还报错继续编译
// 如果你的marker对象中写了错误的字段,也会有这个错误提示

    let _this = this;
    let tempMarkers = [];
    let chooseObj = null;
    
    this.data.markers.forEach(function (obj) {
      if (obj.id == e.detail.markerId) {
        // 这里设置用户点击的对话框显示出来。
        obj.customCallout.display = 'ALWAYS';
        chooseObj = obj;
        // 这里把原来的对象传递,不要放到数组中,等下面的接口获取数据后再放入对象中
      } else {
        obj.customCallout.display = 'BYCLICK';
        tempMarkers.push(obj);
      }
    });

    wx.request({
      url: app.globalData.api + '/getIndexDeviceInfoByOne?id=' + chooseObj.devId,
      method: 'get',
      data: '',
      header: {
        'Content-Type': 'application/json',
      },
      success: function (res) {
        var response = res.data;
        if (response != null && response.data != null) {
          let onlineStatus = "离线";
          if (response.data.online == 0) {
            onlineStatus = "在线";
          }
          let deviceStatus = "故障";
          if (response.data.devStatus == 0) {
            deviceStatus = "正常";
          }
          chooseObj.customObj.companyName = response.data.companyName;
          chooseObj.customObj.devId = response.data.devId;
          chooseObj.customObj.name = response.data.name;
          chooseObj.customObj.onlineStatus = onlineStatus;
          chooseObj.customObj.deviceStatus = deviceStatus;
            // 数据封装
          tempMarkers.push(chooseObj);
            // 显示弹出框
          _this.setData({
            markers: tempMarkers,
            showInfoBox:true
          });
        }
      },
      fail: function (err) {
        console.error(err);
      }
    });

  },
  
  getAllDeviceList() {
    // 从后台获取设备数据
    let _this = this;
    wx.request({
      url: app.globalData.api + '/listMap',
      method: 'get',
      data: _this.queryParams,
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        var response = res.data;
        if (response.code == 401) {
            // 异常后调到登录页面
          wx.redirectTo({
            url: "../loginPhone",
          });
        } else {
          if (response != null && response.rows != null) {
            let tempMarkers = [];
            for (let i = 0; i < response.rows.length; i++) {
                // 自己的离线和在线图片
              let pngColorName = "map_pink";
              if (response.rows[i].onlineFlag == 0) {
                pngColorName = "map_gree";
              }
              let pngColorPath = "../image/" + pngColorName + ".png";
              const markerObj = {
                id: (i + 1),//这里的id必须是数字,所以用下标代替
                devId: response.rows[i].id,
                onlineFlag: response.rows[i].onlineFlag,
                latitude: response.rows[i].latitude,
                longitude: response.rows[i].longitude,
                width: "26px",// 设置图片的大小
                height: "26px",
                iconPath: pngColorPath,
                customCallout: {// 自定义的弹出框
                  anchorY: 0,
                  anchorX: 0,
                  display: 'BYCLICK'// 默认都不显示,markertapFun方法中设置显示
                },
                customObj: {// 自定义携带的数据,便于弹出框上使用,名字随便写
                  companyName: "",
                  devId: "",
                  name: "",
                  onlineStatus: "",
                  deviceStatus: ""
                }
              };
                
              tempMarkers.push(markerObj);
            }
            // 把所有的设备图标显示在地图上
            _this.setData({
              markers: tempMarkers
            });
          }
        }
      },
      fail: function (err) {
        console.error(err);
      }
    });
  },
  // 事件处理函数
  bindViewTap() {
    
  },
  onReady: function () {},
  onLoad() {
    // 页面加载的时候请求后台数据
    this.getAllDeviceList();
    
  },
  onShow: function () {
    
  }
})

index.wxss

.customCallout{
  background: #304156;
		padding: 7px;
		font-size: 14px;
		color: #ffffff;
		border-radius: 6px;
		font-family: initial;
}
.calloutCustomTitle{
  border-bottom:1px solid #ffffff; 
  margin-bottom: 5px;
}

总结

1、教程解决了用户从后台读取数据动态显示marker,然后用户点击marker的时候再次从后台读取数据详情,然后显示customCallout。

2、教程解决了Do not have markertapFun handler in component: pages/index/index. Please make sure that markertapFun handler has been defined in pages/index/index.


// 错误消息
Do not have markertapFun handler in component: pages/index/index. Please make sure that markertapFun handler has been defined in pages/index/index.

// 解决方法
1、如果你确定你的对象中的字段都没有写错,那就再次编译,一直到看到正确的效果,否则一直编译。
2、customCallout对象只有三个字段,如果增加了其它字段或者写错了,都会报错
customCallout: {
                  anchorY: 0,
                  anchorX: 0,
                  display: 'BYCLICK'
                },

3、 customCallout显示后,单击地图可以隐藏弹出框,需要在bindtap方法中实现。

 

结束

-----华丽的分割线,以下是凑字数,大家不用花时间看,快去改代码-----

-----华丽的分割线,以下是凑字数,大家不用花时间看,快去改代码-----

-----华丽的分割线,以下是凑字数,大家不用花时间看,快去改代码-----

package cn.renkai721.bean.vo;
 
import lombok.extern.slf4j.Slf4j;
 
@Slf4j
public class MakeUpTheWordCount {
 
    private String make_up_the_word_count_column_999999999_1;
    private String make_up_the_word_count_column_999999999_2;
    private String make_up_the_word_count_column_999999999_3;
    private String make_up_the_word_count_column_999999999_4;
    private String make_up_the_word_count_column_999999999_5;
    private String make_up_the_word_count_column_999999999_6;
    private String make_up_the_word_count_column_999999999_7;
    private String make_up_the_word_count_column_999999999_8;
    private String make_up_the_word_count_column_999999999_9;
    private String make_up_the_word_count_column_999999999_10;
    private String make_up_the_word_count_column_999999999_11;
    private String make_up_the_word_count_column_999999999_12;
    private String make_up_the_word_count_column_999999999_13;
    private String make_up_the_word_count_column_999999999_14;
    private String make_up_the_word_count_column_999999999_15;
    private String make_up_the_word_count_column_999999999_16;
    private String make_up_the_word_count_column_999999999_17;
    private String make_up_the_word_count_column_999999999_18;
    private String make_up_the_word_count_column_999999999_19;
    private String make_up_the_word_count_column_999999999_20;
 
    public String getMake_up_the_word_count_column_999999999_1() {
        return make_up_the_word_count_column_999999999_1;
    }
 
    public void setMake_up_the_word_count_column_999999999_1(String make_up_the_word_count_column_999999999_1) {
        this.make_up_the_word_count_column_999999999_1 = make_up_the_word_count_column_999999999_1;
    }
 
    public String getMake_up_the_word_count_column_999999999_2() {
        return make_up_the_word_count_column_999999999_2;
    }
 
    public void setMake_up_the_word_count_column_999999999_2(String make_up_the_word_count_column_999999999_2) {
        this.make_up_the_word_count_column_999999999_2 = make_up_the_word_count_column_999999999_2;
    }
 
    public String getMake_up_the_word_count_column_999999999_3() {
        return make_up_the_word_count_column_999999999_3;
    }
 
    public void setMake_up_the_word_count_column_999999999_3(String make_up_the_word_count_column_999999999_3) {
        this.make_up_the_word_count_column_999999999_3 = make_up_the_word_count_column_999999999_3;
    }
 
    public String getMake_up_the_word_count_column_999999999_4() {
        return make_up_the_word_count_column_999999999_4;
    }
 
    public void setMake_up_the_word_count_column_999999999_4(String make_up_the_word_count_column_999999999_4) {
        this.make_up_the_word_count_column_999999999_4 = make_up_the_word_count_column_999999999_4;
    }
 
    public String getMake_up_the_word_count_column_999999999_5() {
        return make_up_the_word_count_column_999999999_5;
    }
 
    public void setMake_up_the_word_count_column_999999999_5(String make_up_the_word_count_column_999999999_5) {
        this.make_up_the_word_count_column_999999999_5 = make_up_the_word_count_column_999999999_5;
    }
 
    public String getMake_up_the_word_count_column_999999999_6() {
        return make_up_the_word_count_column_999999999_6;
    }
 
    public void setMake_up_the_word_count_column_999999999_6(String make_up_the_word_count_column_999999999_6) {
        this.make_up_the_word_count_column_999999999_6 = make_up_the_word_count_column_999999999_6;
    }
 
    public String getMake_up_the_word_count_column_999999999_7() {
        return make_up_the_word_count_column_999999999_7;
    }
 
    public void setMake_up_the_word_count_column_999999999_7(String make_up_the_word_count_column_999999999_7) {
        this.make_up_the_word_count_column_999999999_7 = make_up_the_word_count_column_999999999_7;
    }
 
    public String getMake_up_the_word_count_column_999999999_8() {
        return make_up_the_word_count_column_999999999_8;
    }
 
    public void setMake_up_the_word_count_column_999999999_8(String make_up_the_word_count_column_999999999_8) {
        this.make_up_the_word_count_column_999999999_8 = make_up_the_word_count_column_999999999_8;
    }
 
    public String getMake_up_the_word_count_column_999999999_9() {
        return make_up_the_word_count_column_999999999_9;
    }
 
    public void setMake_up_the_word_count_column_999999999_9(String make_up_the_word_count_column_999999999_9) {
        this.make_up_the_word_count_column_999999999_9 = make_up_the_word_count_column_999999999_9;
    }
 
    public String getMake_up_the_word_count_column_999999999_10() {
        return make_up_the_word_count_column_999999999_10;
    }
 
    public void setMake_up_the_word_count_column_999999999_10(String make_up_the_word_count_column_999999999_10) {
        this.make_up_the_word_count_column_999999999_10 = make_up_the_word_count_column_999999999_10;
    }
 
    public String getMake_up_the_word_count_column_999999999_11() {
        return make_up_the_word_count_column_999999999_11;
    }
 
    public void setMake_up_the_word_count_column_999999999_11(String make_up_the_word_count_column_999999999_11) {
        this.make_up_the_word_count_column_999999999_11 = make_up_the_word_count_column_999999999_11;
    }
 
    public String getMake_up_the_word_count_column_999999999_12() {
        return make_up_the_word_count_column_999999999_12;
    }
 
    public void setMake_up_the_word_count_column_999999999_12(String make_up_the_word_count_column_999999999_12) {
        this.make_up_the_word_count_column_999999999_12 = make_up_the_word_count_column_999999999_12;
    }
 
    public String getMake_up_the_word_count_column_999999999_13() {
        return make_up_the_word_count_column_999999999_13;
    }
 
    public void setMake_up_the_word_count_column_999999999_13(String make_up_the_word_count_column_999999999_13) {
        this.make_up_the_word_count_column_999999999_13 = make_up_the_word_count_column_999999999_13;
    }
 
    public String getMake_up_the_word_count_column_999999999_14() {
        return make_up_the_word_count_column_999999999_14;
    }
 
    public void setMake_up_the_word_count_column_999999999_14(String make_up_the_word_count_column_999999999_14) {
        this.make_up_the_word_count_column_999999999_14 = make_up_the_word_count_column_999999999_14;
    }
 
    public String getMake_up_the_word_count_column_999999999_15() {
        return make_up_the_word_count_column_999999999_15;
    }
 
    public void setMake_up_the_word_count_column_999999999_15(String make_up_the_word_count_column_999999999_15) {
        this.make_up_the_word_count_column_999999999_15 = make_up_the_word_count_column_999999999_15;
    }
 
    public String getMake_up_the_word_count_column_999999999_16() {
        return make_up_the_word_count_column_999999999_16;
    }
 
    public void setMake_up_the_word_count_column_999999999_16(String make_up_the_word_count_column_999999999_16) {
        this.make_up_the_word_count_column_999999999_16 = make_up_the_word_count_column_999999999_16;
    }
 
    public String getMake_up_the_word_count_column_999999999_17() {
        return make_up_the_word_count_column_999999999_17;
    }
 
    public void setMake_up_the_word_count_column_999999999_17(String make_up_the_word_count_column_999999999_17) {
        this.make_up_the_word_count_column_999999999_17 = make_up_the_word_count_column_999999999_17;
    }
 
    public String getMake_up_the_word_count_column_999999999_18() {
        return make_up_the_word_count_column_999999999_18;
    }
 
    public void setMake_up_the_word_count_column_999999999_18(String make_up_the_word_count_column_999999999_18) {
        this.make_up_the_word_count_column_999999999_18 = make_up_the_word_count_column_999999999_18;
    }
 
    public String getMake_up_the_word_count_column_999999999_19() {
        return make_up_the_word_count_column_999999999_19;
    }
 
    public void setMake_up_the_word_count_column_999999999_19(String make_up_the_word_count_column_999999999_19) {
        this.make_up_the_word_count_column_999999999_19 = make_up_the_word_count_column_999999999_19;
    }
 
    public String getMake_up_the_word_count_column_999999999_20() {
        return make_up_the_word_count_column_999999999_20;
    }
 
    public void setMake_up_the_word_count_column_999999999_20(String make_up_the_word_count_column_999999999_20) {
        this.make_up_the_word_count_column_999999999_20 = make_up_the_word_count_column_999999999_20;
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1795593.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

pESC-HIS是什么,怎么看?-实验操作系列-2

01 典型的pESC-HIS质粒遗传图谱 02 介绍 质粒类型&#xff1a;酿酒酵母蛋白表达载体 表达水平&#xff1a;高拷贝 诱导方法&#xff1a;半乳糖 启动子&#xff1a;GAL1和GAL10 克隆方法&#xff1a;多克隆位点&#xff0c;限制性内切酶 载体大小&#xff1a;6706bp 5 测…

uniapp封装picker选择器组件,支持关键字查询

CommonPicker.vue组件 路径在 components\CommonPicker.vue <template><view><uni-easyinput v-model"searchQuery" :placeholder"placeholder" /><picker :range"filteredOptions" :range-key"text" v-model&…

韩顺平0基础学java——第18天

p374-395 类变量和类方法 类变量&#xff08;静态变量&#xff09; 例&#xff1a; class Child{ public static Int count&#xff1b;//这个count可以被所有Child实例共享 /..../ } 内存中&#xff0c;static在堆中是独立存放的&#xff0c;并不在某个对象的空间中。 由于…

行业分析---造车新势力之理想汽车

1 前言 在之前的博客中&#xff0c;笔者撰写了多篇行业类分析的文章&#xff08;科技新能源&#xff09;&#xff1a; 《行业分析---我眼中的Apple Inc.》 《行业分析---马斯克的Tesla》 《行业分析---造车新势力之蔚来汽车》 《行业分析---造车新势力之小鹏汽车》 此类文章的受…

前端传参数后端变量类型能够接受到List却无法接收到值

问题描述 今天写了个接口&#xff0c;下图所示 ReqVO里是这样的&#xff1a; 然后前端去请求&#xff0c;从请求结果中看发现这里值是在的&#xff08;有经验的可能就看出来了otherInfo.id: 这样以参数后端是接收不到的&#xff0c;但是当时没发现&#xff09; 传进来后端…

zynq-7015启动分析及裸机BootLoader编写(未完待续)

使用lwip-tcp远程对QSPI进行更新、QSPI FLASH启动 W25Q128资料&#xff1a; W25Q128JV datasheet(1/78 Pages) WINBOND | 3V 128M-bit serial flash memory with dual/quad spi (alldatasheet.com) UG585资料&#xff1a; Zynq 7000 SoC Technical Reference Manual-UG585 翻译…

python3 -m http.server 检查打包前端的项目

python3 -m http.server这是 Python 提供的一个内置的简单 HTTP 服务器。当你在终端中运行 python3 -m http.server 命令时(在对应的打包目录比如dist目录)&#xff0c;Python 会启动一个 HTTP 服务器&#xff0c;它会将当前工作目录下的文件作为静态文件提供给浏览器。这个服务…

张大哥笔记:高考,万人过独木桥,你怕不怕摔倒?

今天刷到一个新闻&#xff1a;宁夏煤业计划招600名挖煤的井下操作工&#xff0c;要求大学学历&#xff01;结果却吸引了7900人来报名&#xff01;我都惊呆了&#xff0c;什么时候挖煤都要求这么高的学历了&#xff0c;那读书到底起啥作用&#xff01; 如果一个人读书读到大学后…

web刷题记录(4)

[GKCTF 2020]cve版签到 进来应该是给了个提示了&#xff0c;就是要以.ctfhub.com结尾 还有一个超链接&#xff0c;这题的ssrf还是挺明显的&#xff0c;抓包看看 发现回显里面有提示 说是和本地有关&#xff0c;那么也就是说&#xff0c;要访问127.0.0.1&#xff0c;大概意思就…

基于FPGA的任意点滑动平均(滑动窗长度和数据位宽参数化,例化时参数可设置)

目录 1.前言2.原理3.举例说明4.Matlab实现5.FPGA实现滑动平均 微信公众号获取更多FPGA相关源码&#xff1a; 1.前言 对于一维信号&#xff0c;我们可以使用类似移动平均滤波&#xff08;Moving Average Filtering&#xff09;实现denoising。Moving Average Filtering 是一种…

超速解读多模态InternVL-Chat1.5 ,如何做到开源SOTA——非官方首发核心技巧版(待修订)

解读InternVL-chat1.5系列 最近并行是事情太杂乱了&#xff0c;静下心来看一看优秀的开源项目,但是AI技术迭代这么快&#xff0c;现在基本是同时看五、六个方向的技术架构和代码&#xff0c;哪个我都不想放&#xff0c;都想知道原理和代码细节&#xff0c;还要自己训练起来&am…

性能飙升50%,react-virtualized-list如何优化大数据集滚动渲染

在处理大规模数据集渲染时&#xff0c;前端性能常常面临巨大的挑战。本文将探讨 react-virtualized-list 库如何通过虚拟化技术和 Intersection Observer API&#xff0c;实现前端渲染性能飙升 50% 的突破&#xff01;除此之外&#xff0c;我们一同探究下该库还支持哪些新的特性…

【嵌入式DIY实例】-OLED显示天气数据

OLED显示天气数据 文章目录 OLED显示天气数据1、硬件准备与接线2、天气数据获取准备3、代码实现在这个物联网项目中,本文将展示如何使用 ESP8266 NodeMCU (ESP-12E) Wi-Fi 开发板和 SSD1306 OLED 显示屏(12864 像素)制作一个简单的互联网气象站。 NodeMCU 从天气网站 openwe…

JavaWeb3 Ajax+Axios+Element+Nginx部署

Ajax 异步JS和XML 1.数据交换&#xff1a;给服务器发送请求&#xff0c;并获取服务器相应的数据 2.异步交互&#xff1a;在不重新加载整个页面的情况下&#xff0c;与服务器交换数据并更新部分网页 同步与异步 原生Ajax <!DOCTYPE html> <html> <body><…

kafka-消费者-消费异常处理(SpringBoot整合Kafka)

文章目录 1、消费异常处理1.1、application.yml配置1.2、注册异常处理器1.3、消费者使用异常处理器1.4、创建生产者发送消息1.5、创建SpringBoot启动类1.6、屏蔽 kafka debug 日志 logback.xml1.7、引入spring-kafka依赖1.8、消费者控制台&#xff1a;1.8.1、第一次启动SpringK…

【WP】猿人学_17_天杀的Http2.0

https://match.yuanrenxue.cn/match/17 抓包分析 居然对Fiddler有检测&#xff0c;不允许使用 那就使用浏览器抓包&#xff0c;好像没发现什么加密参数&#xff0c;然后重放也可以成功&#xff0c;时间长了也无需刷新页面&#xff0c;尝试Python复现。 Python复现 import …

在线标注流程

文章目录 在线标注流程标注方法 在线标注流程 登录地址&#xff1a;http://7a27c5e078f644a2a9b734603913c65e.login.bce.baidu.com 出现页面&#xff1a; 登录名&#xff1a; 三个中任意一个 密码&#xff1a;ZNSJ123a 登录之后叉掉。再打开这个网站&#xff1a;https://…

CAD 文件(DXF / DWG)转换为(DXF / PDF / PNG / SVG)

方法一Github 这个是ezdxf出品的&#xff0c;可以使用命令行的方式进行转换 ezdxf draw -o file.<png|svg|pdf> <file.dxf>也可以自己改动代码 examples/addons/drawing/pdf_export.py 但是直接运行会有误&#xff0c;以下是我改动后的代码&#xff1a; from ez…

ArrayList——简单洗牌算法

特殊语法介绍&#xff1a; List<List<E>> 该语法情况比较特殊&#xff0c;相当于一个“二维数组”存着一个个线性表的结构&#xff0c;如图&#xff1a; 该语法的灵活性强&#xff0c;可适用于多种类型和多种情况。接下来就使用该语法来实现一个简单的洗牌操作。…

【Linux取经路】网络套接字编程——初识篇

文章目录 一、端口号1.1 认识端口号1.2 端口号 VS 进程 PID 二、认识 TCP 协议三、认识 UDP四、网络字节序列五、socket 编程接口5.1 常用 API5.2 sockaddr 结构 六、结语 一、端口号 网络通信的本质是应用层软件进行数据的发送和接受&#xff0c;软件在启动之后&#xff0c;本…