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

需求背景

微信小程序开发,需要使用腾讯地图显示自定义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.mzph.cn/bicheng/23998.shtml

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

相关文章

SSM旅游论坛(前后分离源码+论文)

该旅游论坛是基于Spring、SpringMVC、Mybatis框架开发出来的 用户信息管理 此页面提供给管理员的功能有&#xff1a;用户信息的查询管理&#xff0c;可以删除用户信息、修改用户信息、新增用户信息&#xff0c; 还进行了对用户名称的模糊查询的条件 景点信息管理 论坛类型管理…

医诊合作怎么避免减少双方损失,吸引更多的优质医疗资源下沉到基层?防漏费系统又起到什么作用?

健康领域的现代化治理要求在公共医疗卫生机构之间建立合作关系,然而我国公共医疗卫生服务领域呈现各级各类健康服务机构自由混合竞争的格局,这对公医分类分级诊疗法治秩序构成全面挑战,并成为影响转诊制度构建的重要因素.我国转诊制度的建设应以维护诊疗合作的法律秩序为前提,并…

Git概念用法

Git是一种版本控制系统&#xff0c;它可以追踪文件的修改并保存这些修改的历史记录。以下是Git的基本概念和使用方式&#xff1a; 仓库&#xff08;Repository&#xff09;&#xff1a;仓库是存储代码和版本历史记录的地方。可以在本地或远程服务器上创建仓库。 提交&#xff…

帕友饮食小建议,健康美味两不误

亲爱的帕友们&#xff0c;大家好&#xff01; 在忙碌的生活中&#xff0c;饮食健康往往是我们容易忽视的一环。作为帕友&#xff0c;我们更应该注重饮食的均衡与营养&#xff0c;让身体得到充分的滋养。今天&#xff0c;就来给大家分享一些帕友饮食的小建议&#xff0c;让我们…

【x264】码率控制模块的简单分析—编码主流程

【x264】码率控制&#xff08;rate control&#xff09;模块的简单分析—编码主流程 1. 码率控制概述1.1 比特分配1.2 率失真优化&#xff08;RDO&#xff09; 2.码率控制中比特分配的实现2.1 码率控制器的创建&#xff08;x264_ratecontrol_new&#xff09;2.1.1 码控模块的重…

项目工具|git相关

本博客暂时只作为个人资料&#xff0c;后续会进行完善&#xff0c;主要内容来自&#xff1a; 【【Git第一讲】&#xff1a;git分区与两个盒子的故事】 理解暂存区和未暂存区 git为什么要多一个暂存区&#xff1f;难道不能我把代码写完后就是未暂存区&#xff0c;然后直接提交…

【C++PCL】点云处理点到面ICP配准

作者:迅卓科技 简介:本人从事过多项点云项目,并且负责的项目均已得到好评! 公众号:迅卓科技,一个可以让您可以学习点云的好地方 重点:每个模块都有参数如何调试的讲解,即调试某个参数对结果的影响是什么,大家有问题可以评论哈,如果文章有错误的地方,欢迎来指出错误的…

VS2015安装模块时报错:安装包丢失或损坏,,,,,无效的驱动器

解决&#xff1a; 1、解压.iso文件&#xff1b; 2、替换目录 替换为解压后的对应的目录&#xff0c;点击重试即可。

Mysql学习(三)——SQL通用语法之DML

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 DML添加数据修改数据删除数据 总结 DML DML用来对数据库中表的数据记录进行增删改操作。 添加数据 -- 给指定字段添加数据 insert into 表名(字段1&#xff0c;字…

关于调用百度AI开放平台短文本在线合成API接口 Open api characters limit reached 问题

官方示例代码中心&#xff1a; https://console.bce.baidu.com/support/?_1668482508529#/api?productAI&project语音技术&parent语音合成&apitext2audio&methodpost 一、调试时&#xff0c;若提示 16: Open api characters limit reached&#xff08;{"…

Highcharts 条形图:数据可视化利器

Highcharts 条形图:数据可视化利器 引言 在数据分析和信息展示领域,图表发挥着至关重要的作用。它们能够将复杂的数据以直观、易于理解的方式呈现给用户。Highcharts 是一个流行的 JavaScript 图表库,广泛用于创建交互式图表。其中,条形图作为一种基础但功能强大的图表类…

线程及其应用

1.线程概念及其用途和优缺点 2.互斥量&#xff08;锁&#xff09; 3.条件变量 4.POSIX信号量 5.生产者消费者模型 6.线程池 7.线程安全的单例模式 1.线程概念及其用途和优缺点 概念&#xff1a;线程是比进程更轻量化的一种执行流&#xff0c;是在进程内部执行的一种执行…

Redis数据类型深掘:如何有效使用List,Set和Sorted Set

List(列表) List 数据类型的深度解析 定义复习 Redis的List数据类型是一个字符串的序列集合,这些字符串按照插入的顺序进行排序。得益于它的设计,List允许重复的值出现,并支持两端的推入(push)和弹出(pop)操作。这种数据类型非常适合用作堆栈(Stack)或队列(Queue)。 …

基于工业互联网打造敏捷供应链的实现方式:创新路径与实践应用

引言 工业互联网和敏捷供应链是当今制造业发展中的两个重要概念。工业互联网以数字化、网络化和智能化为核心&#xff0c;致力于将传统工业生产与互联网技术相融合&#xff0c;从而实现生产过程的高效、智能和灵活。而敏捷供应链则强调快速响应市场需求、灵活调整生产和供应计划…

fastadmin批量导入

表的字段必须备注清楚导出的excel表头必须对应上如果mysql表有约束&#xff0c;导入会自动限制&#xff0c;挺方便的一个功能。

上心师傅的思路分享(二)

Druid monitor 与Springboot常见报错界面渗透小技巧 目录 前言 1.Druid monitor介绍 2.Druid未授权(1rank) 3.druid弱口令 4.Druid进一步利用 4.1 URL监控 4.2 Session监控 利用思路 EditThisCookie(小饼干插件) 5.SpringBoot Actuator未授权访问漏洞 5.1 简介 5…

微信群聊天机器人怎么搭建

要使用 chatgpt-on-wechat 项目搭建一个微信群聊机器人并获取群聊信息&#xff0c;请按照以下步骤操作&#xff1a; 克隆仓库&#xff1a; git clone https://github.com/zhayujie/chatgpt-on-wechat cd chatgpt-on-wechat/安装依赖&#xff1a; pip3 install -r requirements.…

深入理解Linux中的`as`命令:汇编器之旅

标题&#xff1a;深入理解Linux中的as命令&#xff1a;汇编器之旅 在Linux的世界中&#xff0c;编程和编译过程通常涉及多个步骤&#xff0c;从源代码到可执行文件&#xff0c;每一步都至关重要。其中一个重要的步骤是将汇编代码转换为机器代码&#xff0c;这通常是由汇编器&a…

Spark MLlib机器学习

前言 随着大数据时代的到来&#xff0c;数据处理和分析的需求急剧增加&#xff0c;传统的数据处理工具已经难以满足海量数据的分析需求。Apache Spark作为一种快速、通用的集群计算系统&#xff0c;迅速成为了大数据处理的首选工具。而在Spark中&#xff0c;MLlib&#xff08;…

【Java数据结构】详解LinkedList与链表(三)

&#x1f512;文章目录&#xff1a; 1.❤️❤️前言~&#x1f973;&#x1f389;&#x1f389;&#x1f389; 2.无头双向非循环链表的实现 2.1成员属性 2.2成员方法 display——打印链表 size——获取单链表长度 addFirst——头插 addLast——尾插 addIndex——在任…