百度地图API使用误点之手动设置地图缩放

在使用百度地图时我遇到了一个超奇怪的问题

label.addEventListener("click", () => {
                    var geocoder = new BMap.Geocoder();
                    let zoom = this.map.getZoom();
                    console.log('zoom',zoom);
                    geocoder.getLocation(point, res => {
                      //根据坐标解析地名
                      // this.block = false;
                      if(zoom < 11) {
                        this.city = res.addressComponents.city;
                        this.place = res.addressComponents.city;
                        this.map.centerAndZoom(point, 11);
                        this.refresh();
                      }else {
                        console.log(this.map.getZoom());
                        console.log('block',this.block);
                        this.block = true;
                        this.place = res.addressComponents.district;
                        this.city = res.addressComponents.city;
                        this.map.centerAndZoom(point, 14);
                        console.log('block',this.block);
                        console.log(this.map.getZoom());
                        this.refresh();
                      }
                    });

我在百度地图里面设置了点击标签改变地图缩放层级的事件
浏览器中的打印是这样的

image.png

但是我的block明明设置的为true啊
this.map.centerAndZoom(point, 14);
中间走了这行代码
原来我的代码中还有浏览器缩放事件

// 缩放结束
      this.map.addEventListener("zoomend", e => {
        // console.log("e", e);
        alert('zoomed');
        let zoom = this.map.getZoom();
        let center = this.map.getCenter();
        this.block = false;
        if (zoom < 11) {
          if (this.place != "浙江省") {
            this.city = "浙江省";
            this.place = "浙江省";
            this.refresh();
          }
        } else if (zoom < 13) {   
          geocoder.getLocation(center, res => {
            if (res.addressComponents.province == "浙江省") {
              if (this.place != res.addressComponents.city) {
                this.place = res.addressComponents.city;
                this.city = res.addressComponents.city;
                this.refresh();
              }
            }
          });
        } else {
          // console.log('123');
          geocoder.getLocation(center, res => {
            //获取位置
            if (res.addressComponents.province == "浙江省") {
              this.block = true;
              if (this.place != res.addressComponents.district) {
                this.place = res.addressComponents.district;
                this.city = res.addressComponents.city;
                this.refresh();
              }
            }
          });
        }
      });

这个事件会调用地图缩放事件,所以将block改为了false
真的是坑啊

你可能感兴趣的:(百度地图API使用误点之手动设置地图缩放)