Javascript 如何在IONIC2中添加标记单击侦听器?

Javascript 如何在IONIC2中添加标记单击侦听器?,javascript,google-maps,typescript,ionic-framework,ionic2,Javascript,Google Maps,Typescript,Ionic Framework,Ionic2,我正在使用IONIC2,我已经实现了地图并在地图上放置了标记。 我可以获取映射单击侦听器,但无法获取标记单击侦听器 我也尝试了addlistener,但无法获取标记的click事件 以下是我在地图上添加标记的代码: addMarkerOnMap(latLng1: GoogleMapsLatLng, depotsEntity: DepotsEntity) { /** * To add marker in the map, need to initialize GoogleMapsMarkerOp

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

我也尝试了addlistener,但无法获取标记的click事件

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

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中的marker click listener,请帮助我。

查看您的代码,我假设您正在使用

有几件事你做错了。第一个是
map.addMarker()
不返回标记,而是返回一个带有
GoogleMapsMarker
类型值的承诺,这使得此行不正确

let marker=this.map.addMarker(选项)

其次,将事件处理程序附加到标记对象,而不是映射,这意味着此位也是错误的

this.map.on(this.map.markerClicked, function () { });
您需要做的是调用
this.map.addMarker()
,等待承诺得到解决,然后将单击处理程序添加到承诺返回的标记中

示例

this.map.addMarker(options).then((marker: GoogleMapsMarker) => {
        marker.addEventListener(GoogleMapsEvent.MARKER_CLICK).subscribe(() => { console.log('Marker clicked...'); });
});
您需要确保
GoogleMapsMarker
GoogleMapsEvent
已从
ionic native
导入

注意

this.map.addMarker(options).then((marker: GoogleMapsMarker) => {
        marker.addEventListener(GoogleMapsEvent.MARKER_CLICK).subscribe(() => { console.log('Marker clicked...'); });
});
在写这篇文章时,我已经从理论上向您展示了如何实现您想要的功能,但目前这可能会给您带来以下错误

此.\u next不是一个函数

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