maptalks 单独移除图层 ,添加点击事件

发布于:2023-01-22 ⋅ 阅读:(12) ⋅ 点赞:(0) ⋅ 评论:(0)

vue-mapmalks单独移除图层 ,添加点击事件

图层

添加和移除

  添加图层1 this.layer = new maptalks.VectorLayer("vector").addTo(this.map);
  添加图层2 this.carlayer = new maptalks.VectorLayer("car").addTo(this.map);

根据自己需要,定义不同的图层,作者要清除 this.carlayer ,因为一个图层里面多个图片标注是无法区分操作的,所以定义了两个图层。
移除标注图层 this.map.removeLayer(this.carlayer);

标注点击事件带参数

setInfoWindow() 官方弹窗跳过

1.添加标注的时候把参数传进去
在这里插入图片描述
2.添加地图点击监听事件
this.map.on(“click”, function (e) {
console.log(e);
that.getData(e);
});
funtion getdata(e){
let that = this;
that.map.identify(
{
coordinate: e.coordinate,
layers: [that.carlayer], //应为独立处理carlayer 图层
},
function (geos) {
if (geos.length === 0) {
return;
}

        let carInfo = geos[0].properties.name;
        that
          .getCarInfo(carInfo)   //去调用后台接口返回自己要的数据
          .then(() => {
            var carAllContent = `<div class="custom_box"><div class="custom_title">${carInfo}--详细信息</div>
                                 <div class="custom_car_marker">
                                        <div>${carContent} <br>${crashContent} </div>
                                </div></div>`;

            that.mapPointList.forEach(function (p) {
              if (p.properties.name == carInfo) {
                p.setInfoWindow({
                  title: "",
                  content: carAllContent,
                  autoPan: true,
                  width: 300,
                  minHeight: 120,
                  custom: true,
                  autoOpenOn: "click",
                  autoCloseOn: "click",
                });
                p.openInfoWindow();
              }
            });
          });