GoogleMap infowindow按下按钮即可点击

时间:2018-06-14 23:43:30

标签: javascript google-maps typescript ionic-framework

我正在使用Googlemap JS API,我有一些从我的数据库中获取的标记和一个infowindow foreach标记,在每个infowindow我有一个按钮希望推送到具有某个变量的另一个页面,我试图使用ngZone,我不是很熟悉有了它,但我尝试了x),这是我的代码:

  // GET request to get data from database
  for(var i=0;i<data.length;i++)
  {

   var ouvrier=data[i].firstname+' '+data[i].lastname; //Getting data from db

   var Latlng = new google.maps.LatLng(data[i].lat,data[i].lon);
     var marker = new google.maps.Marker({
      position: Latlng,
      map: this.map,
      labelAnchor: new google.maps.Point(22, 0),
      draggable:false,
      icon: { url : 'assets/imgs/worker.png',scaledSize: new google.maps.Size(70, 70)
    },

    });




    var infowindow = new google.maps.InfoWindow({
      content: "Name: "+ouvrier+" <input type='button' id='clickableItem' value='consulter profile' >"
    }); 

    google.maps.event.addListener(infowindow, 'domready', () => {

      var clickableItem = document.getElementById('clickableItem');
      clickableItem.addEventListener('click', () => {
        this.zone.run( () => { //Im using ngZone
          console.log("wlidoz "+ data[i].idprof);
        this.navCtrl.push(ProfilPage, {
          profilId: data[i].idprof // PROBLEM HERE
      });

        });


      });
    });

  marker.addListener('click', event => {
    infowindow.open(this.map, marker);
    });

}

一切正常,地图正在加载......等等 但是,当我点击那个按钮时,没有任何反应,我试图在ngZone中使用data[i].idprof的console.log但是未定义,但在监听器之前,console.log正在运行

1 个答案:

答案 0 :(得分:0)

我修复了它,我刚刚在google.maps.event.addLister

之前声明了一个变量
let idz=data[i].idprof;
    let that=this;
    google.maps.event.addListener(infowindow, 'domready', () => {

      var clickableItem = document.getElementById('clickableItem');
      clickableItem.addEventListener('click', () => {
        that.zone.run( () => {
        that.navCtrl.push(ProfilPage, {
          profilId: idz
      });

        });


      });
    });

如果有人有更好的解决方案,我会接受