小程序自定义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;
    }
}

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
自定义高德地图Marker,可以按照以下步骤操作: 1. 创建MarkerOptions对象,设置Marker的经纬度、图标等属性。 2. 通过AMap类的addMarker方法将Marker添加到地图上。 3. 通过AMap类的setInfoWindowAdapter方法设置Marker的信息窗口。 4. 通过AMap类的setOnMarkerClickListener方法设置Marker的点击事件。 具体实现代码如下: ``` // 创建MarkerOptions对象 MarkerOptions markerOptions = new MarkerOptions(); LatLng latLng = new LatLng(39.906901, 116.397972); markerOptions.position(latLng); markerOptions.title("Marker标题"); markerOptions.snippet("Marker描述"); markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_icon)); // 添加Marker地图Marker marker = aMap.addMarker(markerOptions); // 设置Marker的信息窗口 aMap.setInfoWindowAdapter(new AMap.InfoWindowAdapter() { @Override public View getInfoWindow(Marker marker) { View view = getLayoutInflater().inflate(R.layout.marker_info_window, null); TextView titleTextView = view.findViewById(R.id.title); TextView snippetTextView = view.findViewById(R.id.snippet); titleTextView.setText(marker.getTitle()); snippetTextView.setText(marker.getSnippet()); return view; } @Override public View getInfoContents(Marker marker) { return null; } }); // 设置Marker的点击事件 aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { marker.showInfoWindow(); return true; } }); ``` 其中,R.drawable.marker_icon是你自定义Marker图标。marker_info_window是你自定义Marker信息窗口布局。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

renkai721

谢谢您的打赏!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值