问题描述
我正在尝试将 onclick 事件添加到 Marker 我的代码如下所示:
<MarkerWithLabel
position={{ lat: val.coordinate.lat,lng: val.coordinate.lon }}
key={val.thumbnailUrl}
labelAnchor={new google.maps.Point(0,0)}
labelClass={styles.marker_label}
onClick={(e)=>console.log( e )}
>
<div className={styles.preview}>
<img src={val.thumbnailUrl} alt={val.name} />
<p>{val.ratePlan.price.current}</p>
<p><b>{`${val.address.countryName},${val.address.locality},${val.address.region},${val.address.streetAddress}`}</b></p>
<p>rating: {val.guestReviews.rating}</p>
</div>
</MarkerWithLabel>
我的活动没有目标。
我在cosole看到这个
_.gp {latLng: _.I,domEvent: MouseEvent,pixel: _.N,Xa: undefined}
解决方法
我使用 try catch 解决了这个问题。 这是我的代码
<MarkerWithLabel
position={{ lat: val.coordinate.lat,lng: val.coordinate.lon }}
key={val.thumbnailUrl}
labelAnchor={new google.maps.Point(0,0)}
// labelClass={val.id == GlobalStore.propertySelected ? styles.marker_label : styles.marker_label_selected}
labelClass={ styles.marker_label}
icon = {icon}
title={val.id.toString()}
onClick={(e)=>GlobalStore.setPropertySelected( e )}
>
<div className={styles.preview}>
{val.ratePlan.price.current}
</div>
</MarkerWithLabel>
setPropertySelected(e){
try {
if(e.currentTarget.title) {
console.log(e.currentTarget)
}
} catch(error) {
console.log(error);
}
}
现在可以了