1

我正在使用 react google map react-google-maps,并在地图上添加了叠加层。当我单击覆盖时,它会触发位于覆盖 div 后面的位置的 google 信息窗口,因为 MapPanes.mapPane 的优先级高于 overlayLayer。

点击覆盖触发谷歌信息窗口

我从这里https://groups.google.com/forum/#!topic/google-maps-js-api-v3/726OKUJCG6s找到了一个解决方案,但这是纯 JavaScript 实现。

我如何添加google.maps.event.addDomListener(div, 'click', cancelEvent )react-google-maps (也许使用 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>
        );
    }
}
4

0 回答 0