【问题标题】:React Leaflet LayerGroup.Collision PluginReact Leaflet LayerGroup.Collision 插件
【发布时间】:2017-08-05 12:15:40
【问题描述】:

这是我的组件

import React, { PropTypes } from 'react';
import { MapLayer } from 'react-leaflet';
import L from 'leaflet';
import './collision.js';

export default class LayerGroupCollision extends MapLayer {
  static childContextTypes = {
    layerContainer: PropTypes.shape({
      addLayer: PropTypes.func.isRequired,
      removeLayer: PropTypes.func.isRequired,
    })
  }

  getChildContext() {
    return {
      layerContainer: this.leafletElement,
    }
  }
  createLeafletElement() {
    var t = L.layerGroup.collision({ margin: 20 }, this.getOptions());
    this.layerContainer.addLayer(t);
    return t;
  }

  componentDidMount() {
    const {map} = this.context;
    map.on("zoomend", this._onZoomEnd);
  }

  componentWillUnmount() {
    const {map} = this.context;
    map.off("zoomend", this._onZoomEnd);
  }
}

但这不起作用,我只是得到标记。 在缩放时我得到错误

未捕获的类型错误:无法读取未定义的属性“调用”

at NewClass.fire (leaflet-src.js:587)
at NewClass._moveEnd (leaflet-src.js:3432)
at NewClass.<anonymous> (leaflet-src.js:3869)

有什么建议吗?

【问题讨论】:

    标签: leaflet react-leaflet


    【解决方案1】:

    您没有在您的类中定义_onZoomEnd 函数,而您从中扩展的 MapLayer 类也没有它。 但是在map.on("zoomend", this._onZoomEnd)map.off("zoomend", this._onZoomEnd) 中,您将其作为 zoomend 事件的回调提供。 然后调用_onZoomEnd失败,因为它是未定义的

    【讨论】:

    【解决方案2】:

    插件本身负责定义侦听器,因此您可以省略该代码。下面的代码应该足以将 Leaflet.LayerGroup.Collision 插件包装在 React 组件中。由于您收到错误消息的原因,Alex 的评论是正确的。

    import React from 'react';
    import { func, shape } from 'prop-types';
    import { MapLayer } from 'react-leaflet';
    import 'leaflet.layergroup.collision';
    import L from 'leaflet';
    
    export default class LayerGroupCollision extends MapLayer {
      static childContextTypes = {
        layerContainer: shape({
          addLayer: func.isRequired,
          removeLayer: func.isRequired,
        }),
      }
    
      getChildContext() {
        return {
          layerContainer: this.leafletElement,
        };
      }
    
      createLeafletElement() {
        const layerGroup = L.layerGroup.collision({ margin: 4 });
        this.layerContainer.addLayer(layerGroup);
        return layerGroup;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-20
      • 2022-01-09
      • 2021-06-23
      • 1970-01-01
      • 1970-01-01
      • 2020-06-13
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多