首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 9& google地图标记点击事件不能正常工作

Angular 9& google地图标记点击事件不能正常工作
EN

Stack Overflow用户
提问于 2020-09-01 20:17:20
回答 1查看 1.2K关注 0票数 0

下面是我的代码,用来初始化地图并添加集群和标记。一切正常,但是标记点击的行为很奇怪,就像事件被立即触发并命中API一样,但全局变量在几秒钟后更新,有时在几分钟后更新。

代码语言:javascript
复制
  renderMap() {

    window['initMap'] = () => {
      this.loadMap();
    };
    if (!window.document.getElementById('google-map-script')) {
      const s = window.document.createElement('script');
      s.id = 'google-map-script';
      s.type = 'text/javascript';
      s.src = 'https://maps.googleapis.com/maps/api/js?key=' + environment.GOOGLE_MAP_API_KEY + '&callback=initMap';

      window.document.body.appendChild(s);
    } else {
      this.loadMap();
    }
  }

  loadMap() {
    const map = new window['google'].maps.Map(this.mapElement.nativeElement, {
      center: {lat: this.center.lat, lng: this.center.lng},
      zoom: this.zoom
    });
    const markers = this.markers.map((location, i) => {
      const marker = new google.maps.Marker({
        position: {lat: location.lat, lng: location.lng},
        label: location.label,
        icon: 'assets/img/marker_.png',
        title: location.title,
        clickable: true
      });

      marker.addListener('click', () => {
        const m = location;
        this.customer = m;
        this.showLocation = true;
        this.loading = true;
        this.center = {
          lat: m.lat,
          lng: m.lng
        };
        this.companyService.company(m.company_id).subscribe((company: any) => {
          this.company = company;
          this.loading = false;
        }, error => {
          this.loading = false;
          console.log(error);
        });
      });
      return marker;
    });

    const markerCluster = new MarkerClusterer(map, markers,
      {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});

  }

  ngOnInt(){
    this.renderMao();
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-01 20:29:26

您可能需要在NgZone中运行click回调

代码语言:javascript
复制
constructor(private zone: NgZone)


marker.addListener('click', () => {
    this.zone.run(() => { 
     ...
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63687502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档