百度地图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
真的是坑啊

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,871评论 1 32
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,431评论 4 61
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 28,241评论 1 45
  • 碧树垂绦风撩起 怒江潮信谁按住 一剑风雷乾坤引 散落春霜四五处
    躺在沙发上的Lee阅读 1,342评论 0 1
  • 失望是自己一次次的欲言又止中不断累积的
    法坦坦阅读 1,522评论 0 0

友情链接更多精彩内容