如何在 google 地图 'marker' 点击后显示 alert()?

How do I display an alert() off of a google maps 'marker' click?

我有:enter image description here ...

google.maps.event.addListener(marker,'click',function() {

  this.map.setZoom(15);
  this.map.setCenter(marker.getPosition());
  console.log('hello world');
  this.presentAlert(); // ERROR core.js:9110 ERROR TypeError: 
  //this.presentAlert is not a function

});

...

我想要的:enter image description here

我的代码展开了: ...

 public addMarker(lat: number, lng: number) {
    //let latLng = new google.maps.LatLng(lat, lng);
    let latLng = new google.maps.LatLng(21.576, -158.271); // hiking 
    // spot

    let marker = new google.maps.Marker({
      map: this.map,
      animation: google.maps.Animation.DROP,
      position: latLng
    });

    this.markers.push(marker); // catch 'em all

    google.maps.event.addListener(marker,'click',function() {

      this.map.setZoom(15);
      this.map.setCenter(marker.getPosition());
      console.log('hello world');
      this.presentAlert(); // ERROR core.js:9110 ERROR TypeError: 
      // this.presentAlert is not a function

    });

  }

presentAlert() {
        this.alertCtrl.create({
          header: 'Alert',
          subHeader: 'Subtitle',
          message: 'This is an alert message.',
          buttons: ['OK']
        }).then(alert=> {
          alert.present();
        });

      }

...

我已经尝试了一些其他的东西,但对我来说,直觉上,这是最有意义的。帮助?请谢谢。

this

使用局部变量

因为 this 根据其运行的上下文更改其值。

public addMarker(lat: number, lng: number) {
  ...

  let that = this;

  google.maps.event.addListener(marker, 'click', function() {
    ...

    that.presentAlert();
  });

}