本文主要是介绍小程序自定义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;}
}
这篇关于小程序自定义marker弹出框教程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!