当您使用Google Maps API点击 map 中的某个位置时,它会显示一个信息窗口,其中包含名称、地址和在Google Maps上查看该位置的链接.
有没有办法覆盖此事件并为其提供我自己的数据和样式?
到目前为止,我添加了一个onClick侦听器,它查找具有placeID属性的事件,获取该位置的数据,并将其显示在InfoWindow组件中.
但是,这并不妨碍打开默认窗口.它仍然开着,然后被我的盖住了.很管用,但并不完全是我想要达到的效果.
我要么希望完全阻止默认的事件处理程序显示,要么将我的更改直接应用到它,而不需要有这个单独的事件处理程序.
async function handleOnClick(event) {
setContent(undefined);
const latitude = event.latLng.lat();
const longitude = event.latLng.lng();
if (event.placeId) {
const request = {
placeId: event.placeId,
fields: ['name', 'formatted_address', 'place_id', 'geometry', 'photo'],
};
const placesService = new google.maps.places.PlacesService(map);
placesService.getDetails(request, (place, status) => {
if (status === 'OK') {
const info = (
<InfoWindow
position={{
lat: place?.geometry?.location?.lat(),
lng: place?.geometry?.location?.lng(),
}}
onCloseClick={() => setContent(undefined)}
>
<div>
<h3>{place.name}</h3>
<div className='photo-gallery'>
{place?.photos.map((photo) => {
return <img className='photo' src={photo.getUrl()} />;
})}
</div>
</div>
</InfoWindow>
);
setContent(info);
}
});
}
}
我在使用@react-google-map/api库的Reaction中执行此操作