如何在 IONIC2 中添加标记点击监听器?

How to add marker click listener in IONIC2?

我正在使用 IONIC2,我已经实现了地图并在地图上放置了标记。 我可以获取地图点击监听器但无法获取标记点击监听器。

我也尝试过 addlistener,但无法获取标记的点击事件。

这是我在地图上添加标记的代码:

addMarkerOnMap(latLng1: GoogleMapsLatLng, depotsEntity: DepotsEntity) {
/**
 * To add marker in the map, need to initialize GoogleMapsMarkerOptions
 */
let options: GoogleMapsMarkerOptions = {
  icon: "www/img/location.png",
  title: depotsEntity.address,
  position: latLng1,
  animation: GoogleMapsAnimation.DROP
};
let marker = this.map.addMarker(options)
this.map.on(this.map.markerClicked, function () {
  console.log("markerClicked"); // This is not working yet

});

如果有人知道 IONIC2 中的标记点击侦听器,请帮助我。

查看您的代码,我假设您正在使用 cordova-plugin-googlemaps

你有几处做错了。第一个是 map.addMarker() 不是 return 标记,而是 return 一个具有 GoogleMapsMarker 类型值的承诺,这使得这一行不正确

let marker = this.map.addMarker(options)

其次,您将事件处理程序附加到标记对象而不是地图,这意味着这一点也是错误的

this.map.on(this.map.markerClicked, function () { });

您需要做的是调用 this.map.addMarker(),等待 promise 被解析,然后将点击处理程序添加到由 promise 编辑的标记 return。

例子

this.map.addMarker(options).then((marker: GoogleMapsMarker) => {
        marker.addEventListener(GoogleMapsEvent.MARKER_CLICK).subscribe(() => { console.log('Marker clicked...'); });
});

您需要确保 GoogleMapsMarkerGoogleMapsEvent 已从 ionic-native 导入。

备注

在撰写本文时,我已经从理论上向您展示了如何实现您想要的功能,但目前这可能会给您带来以下错误

this._next is not a function

这是一个已知问题,据我所知,它会发生在您必须订阅的任何地图/标记事件上。可以在 Ionic Native GitHub 页面 #206.

上跟踪该问题