【问题标题】:react-google-maps and google event listeners - how to catch events?react-google-maps 和 google 事件监听器 - 如何捕捉事件?
【发布时间】:2017-08-27 00:20:24
【问题描述】:

在我的应用程序中,我使用 react-google-maps (v.6.x.x) 来处理 Google 地图的 api。我正在使用标记和信息框来显示正确的信息。 enableEventPropagation 设置为 true 的信息框,通过信息框将事件传播到地图层 - 这是什么意思?当我有信息框 - 也就是我点击它的信息窗口时,下面放置了标记,这个标记首先被“点击”,而不是信息框中的任何 html 元素。如果 enableEventPropagation 为假 - 不会传播任何内容。所以我的问题是 - 是否有可能为反应组件添加谷歌事件监听器,例如代码:

let infobox = (<InfoBox
  onDomReady={() => props.infoBoxReady(infobox)}
  defaultPosition={new google.maps.LatLng(props.activeMarker.location[1], props.activeMarker.location[0])}
  options={{
    closeBoxURL: ``, enableEventPropagation: true, pane: 'mapPane',
    position: new google.maps.LatLng(props.activeMarker.location[1], props.activeMarker.location[0]),
    alignBottom: true,
    pixelOffset: new google.maps.Size(-120, 0)
  }}>

如何使用此代码来使用 Google 事件侦听器

google.maps.event.addListener(infobox, 'domready', function ()

我遇到这种错误

任何线索,我怎样才能为它设置监听器,或者也许还有其他选项可以为谷歌地图设置监听器 - 不幸的是,我必须使用这个库并处理对信息框的点击

【问题讨论】:

    标签: google-maps reactjs


    【解决方案1】:

    我能够阻止点击地图(停止传播)并且仍然能够单击 InfoBox 中的项目的唯一方法是设置“enableEventPropagation:false”,然后为 InfoBox 中的项目添加侦听器“onDomReady”道具。这可确保在 InfoBox 呈现后附加侦听器。不确定这是否是最好的解决方案,但它确实有效。希望对某人有所帮助。

    <InfoBox
      defaultPosition={new LatLng(marker.position.lat, marker.position.lng)}
      onDomReady={
          () => {
              let closeButton = $('a.close');
              let someLink = $('a.info-box-profile');
    
              closeButton.on('click', () => {
                  // do something with the click
              });
    
              someLink.on('click', () => {
                  // do something with the click
              });
          }
      }
      options={{
          pixelOffset: new Size(-145, 30),
          closeBoxURL: '',
          enableEventPropagation: false,
          boxStyle: {
              overflow: "hidden",
              width: "320px",
          }
      }}>
    

    【讨论】:

      【解决方案2】:

      我编写了这个包装器组件,它在整个地图上放置了一个&lt;Rectangle/&gt;,以阻止点击传递到下面的地图。同时仍然允许您单击 &lt;infoBox/&gt; 内的内容。

      这允许将enableEventPropagation 设置为true 而不会产生问题。然后我使用mouseOver 和mouseOut 来控制矩形的工作方式。就我而言,我使用单击矩形来关闭我的&lt;InfoBox/&gt;。您可以轻松地隐藏和显示它。

      /* global google */
      /* eslint-disable jsx-a11y/no-static-element-interactions */
      
      import React from 'react';
      import PropTypes from 'prop-types';
      import { Rectangle } from 'react-google-maps';
      import GoogleInfoBox from 'react-google-maps/lib/components/addons/InfoBox';
      
      const cardWidth = 235;
      const boxShadow = 25; // space for outer shadow
      const iconHeight = 2; // the actual height is 48px but we use 6px instead to hide the icon but keep it's shadow
      
      class InfoBox extends React.Component {
        constructor(props) {
          super(props);
          this.closable = true;
        }
      
        onClose = () => {
          if (this.closable) this.props.onClose();
        }
      
        onHover = () => {
          this.closable = false;
        }
      
        onMouseOut = () => {
          this.closable = true;
        }
      
        render() {
          const { children, position, onClose, type } = this.props;
      
          return (
            <div className="info-box">
              <Rectangle
                options={{ fillColor: '#ffffff', fillOpacity: 0.7, zIndex: 100 }}
                bounds={{ north: 90, south: -90, east: 180, west: -180 }}
                onClick={this.onClose}
              />
              <GoogleInfoBox
                position={new google.maps.LatLng(position.lat, position.lng)}
                onCloseClick={onClose}
                options={{
                  alignBottom: true,
                  disableAutoPan: false,
                  pixelOffset: new google.maps.Size(-(cardWidth / 2) - boxShadow, -iconHeight),
                  maxWidth: width,
                  isHidden: false,
                  visible: true,
                  pane: 'floatPane',
                  enableEventPropagation: true,
                }}
              >
                <div
                  onMouseOver={this.onHover}
                  onFocus={this.onHover}
                  onMouseOut={this.onMouseOut}
                  onBlur={this.onMouseOut}
                >
                  { children }
                </div>
              </GoogleInfoBox>
            </div>
          );
        }
      }
      
      InfoBox.propTypes = {
        children: PropTypes.element.isRequired,
        position: PropTypes.shape({
          lat: PropTypes.number.isRequired,
          lng: PropTypes.number.isRequired,
        }).isRequired,
        onClose: PropTypes.func,
        type: PropTypes.string,
      };
      
      export default InfoBox;
      

      【讨论】:

        【解决方案3】:

        您可以通过 InfoBox 组件上的 props 访问侦听器。

        在这里查看它们:Github docs

        您已经在使用一个 - onDomReady - 还有一个:

          onCloseClick: `closeclick`,
          onContentChanged: `content_changed`,
          onPositionChanged: `position_changed`,
          onZIndexChanged: `zindex_changed`,
        

        【讨论】:

        • 但是,如果你想在你的信息框内添加一个自定义的 clickabe 元素,那将无济于事
        猜你喜欢
        • 2010-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-23
        • 1970-01-01
        • 1970-01-01
        • 2021-07-24
        • 1970-01-01
        相关资源
        最近更新 更多