我正在使用反应谷歌地图react-google-maps,并在地图上添加了叠加层。当我点击叠加层时,它会触发位于叠加div后面的地方的Google信息窗口,因为MapPanes.mapPane的优先级高于overlayLayer。
我在这里找到了一个解决方案https://groups.google.com/forum/#!topic/google-maps-js-api-v3/726OKUJCG6s,但这是纯JavaScript实现的解决方案。
如何在react-google-maps中添加google.maps.event.addDomListener(div, 'click', cancelEvent )
(也许使用refs,但也试过这个)?
以下是我用于生成地图和叠加层的代码
import { withGoogleMap, GoogleMap, InfoWindow, Marker, OverlayView } from "react-google-maps";
const GeolocationGoogleMap = withGoogleMap(props => (
<GoogleMap defaultZoom={16} center={props.center} ref={ props.onGoogleMapRef }>
{
props.markers.map((marker,index) => (
<Marker
key={index}
position={marker.position || {} }
onClick={() => props.onMarkerClick(marker)}
{...marker}
>
{
marker.showInfo && <OverlayView
position={ marker.position }
mapPaneName={OverlayView.OVERLAY_MOUSE_TARGET}
getPixelPositionOffset={getPixelPositionOffset}
onClick={ () => props.onMarkerClosemarker( marker.markerId ) }
ref={ (e) => props.onOverlayRef(e) }
>
{ marker.content }
</OverlayView>
}
</Marker>
))
}
</GoogleMap>
));
class GoogleMapContainer extends Component {
onOverlayRef = ref =>{
console.log( ">>>>>>>>>> OVERLAY REF >>>>>>>>>>>>>>>>>>" );
console.log( ref );
}
render() {
return (
<div>
<GeolocationGoogleMap
containerElement={
<div className="google-map" />
}
mapElement={
<div style={{ height: `100%` }} />
}
onMarkerClick={this.handleMarkerClick.bind(this)}
center={this.state.center}
mapcenter={this.state.mapcenter}
content={this.state.content}
radius={this.state.radius}
markers={this.state.markers}
onMarkerClosemarker={this.handleMarkerClose.bind(this)}
onGoogleMapRef={this.setMapRef}
onOverlayRef={this.onOverlayRef}
>
</GeolocationExampleGoogleMap>
</div>
);
}
}