小程序自定义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; 还进行了对用户名称的模糊查询的条件 景点信息管理 论坛类型管理…

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

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

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

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

项目工具|git相关

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

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;{"…

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

引言 工业互联网和敏捷供应链是当今制造业发展中的两个重要概念。工业互联网以数字化、网络化和智能化为核心&#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…

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

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

Application UI

本节包含关于如何用DevExpress控件模拟许多流行的应用程序ui的教程。 Windows 11 UI Windows 11和最新一代微软Office产品启发的UI。 Office Inspired UI Word、Excel、PowerPoint和Visio等微软Office应用程序启发的UI。 How to: Build an Office-inspired UI manually 本教…

数据分析中的统计学基础及Python具体实现【数据分析】

各位大佬好 &#xff0c;这里是阿川的博客&#xff0c;祝您变得更强 个人主页&#xff1a;在线OJ的阿川 大佬的支持和鼓励&#xff0c;将是我成长路上最大的动力 阿川水平有限&#xff0c;如有错误&#xff0c;欢迎大佬指正 Python 初阶 Python–语言基础与由来介绍 Python–…

layui左侧菜单栏,鼠标悬停显示菜单文字

layui封装的左侧菜单是固定宽度的&#xff0c;且左侧菜单栏在css里改变宽度&#xff0c;效果并不是很好&#xff08;还设计头部菜单栏&#xff09;&#xff0c;如果写js来让菜单栏能够拉伸&#xff0c;也比较麻烦&#xff0c;那怎么最简单的&#xff0c;让用户看到菜单的文字呢…

从混乱到有序:PDM系统如何优化物料编码

在现代制造业中&#xff0c;物料管理是企业运营的核心。物料编码作为物料管理的基础&#xff0c;对于确保物料的准确性、唯一性和高效性至关重要。随着产品种类的不断增加和产品变型的多样化&#xff0c;传统的物料编码管理方式已经不能满足企业的需求。本文将探讨产品数据管理…

SpringSecurity6从入门到实战之默认用户的生成流程

SpringSecurity6从入门到实战之默认用户的生成流程 这次还是如标题所示,上一章我们的登录页面已经知道是如何生成了.那么,我们通过表单登录的user用户以及密码SpringSecurity是如何进行生成的呢? 默认用户生成 让我们把登录流程重新拉回到读取/META-INF/spring/ .imports文件 …

什么是MES系统?有什么作用?

MES系统解决方案是专门针对制造企业设计&#xff0c;在MES系统的应用下&#xff0c;实现专业的工厂、车间生产信息化管理方案&#xff0c;帮助制造企业提高生产效率。针对目前制造行业的生产管理状况&#xff0c;以及提升企业生产效率和企业竞争力的需求&#xff0c;实施MES系统…

《尚庭公寓》项目部署之Docker + Nginx

docker rmi nginx docker pull nginx docker rm -f nginx #先创建一个简易的nginx容器&#xff08;后面会删&#xff09;&#xff0c;然后通过 docker cp命令把容器里面的nginx配置反向拷贝到宿主主机上。 docker run --name nginx -p 80:80 -d nginx# 将容器nginx.conf文件复…

小猪APP分发:高效的APP托管服务分发平台

有没有遇到过这样的尴尬&#xff1f;辛辛苦苦开发了一个APP&#xff0c;却在托管和分发环节卡壳。想想看&#xff0c;花了那么多时间精力开发的APP&#xff0c;却因为分发不顺利而影响用户体验&#xff0c;实在是让人抓狂。而小猪APP分发就成了你最好的选择。 APP封装分发www.…

pypi 发布自己的包

注册pypi个人用户 网址&#xff1a;https://pypi.org 目录结构dingtalk_utils 必须-pkgs- __init__.py .gitignore LICENSE 必须 README.md 必须 requirements.txt setup.py 必须安装依赖 pip install setuptools wheel安装上传工具 pip install twinesetup.py i…