使用反应传单注册事件

时间:2017-01-31 06:21:27

标签: javascript reactjs leaflet react-leaflet

我试图将React-Leaflet合并到我的Create React应用程序中。我能够在基本地图上叠加GeoJSON数据,但我无法在此图层上注册点击。

在调查这个问题时,我发现了以下jsfiddle https://jsfiddle.net/n7jmqg1s/6/,它在点击形状时记录事件,如onEachFeature函数所示:

onEachFeature(feature, layer) {
console.log(arguments)
const func = (e)=>{console.log("Click")};

layer.on({
    click: func
});
}

我尝试将其复制并粘贴到我的反应应用程序中,但它并没有在那里工作。我改变的唯一的事情是而不是window.React / window.LeafletReact我使用了es6导入。我不知道这会导致这个问题,但我认为这是可能的。

我查看了onEachFeature函数的参数。在jsfiddle中,我得到了2个参数 - 看起来是特征和图层数据。然而,在我复制的例子中,我得到3个参数,前两个是空的,第三个包含一个包含很多东西的对象,包括(enqueueCallback) : (publicInstance,callback,callerName))

我意识到这有些模糊,但我希望这个问题很容易被识别为对React或传单的误解。我认为这与我没有通过正确的范围或直接操纵DOM或其​​他事实有关。但我不确定。我非常感谢任何帮助。

这是我的组件代码:

    import React from 'react';
import { Map, TileLayer, Marker, Popup, GeoJSON } from 'react-leaflet';

export default class SimpleExample extends React.Component {
  constructor() {
    super();
    this.state = {
      lat: 51.505,
      lng: -0.09,
      zoom: 8,
    };
  }

 onEachFeature = (feature, layer) => {
    layer.on({
      click: this.clickToFeature.bind(this)
    });
  }

  clickToFeature = (e) => {
     var layer = e.target;
     console.log("I clicked on " ,layer.feature.properties.name);

  }

  render() {
    const position = [this.state.lat, this.state.lng];
    const geojsonObject = {
        'type': 'FeatureCollection',
        'crs': {
          'type': 'name',
          'properties': {
            'name': 'EPSG:3857'
          }
        },
        'features': [{
          'type': 'Feature',
          'geometry': {
            'type': 'MultiPolygon',
            'coordinates': [
              [[[-0.09, 51.505], [-0.09, 51.59], [-0.12, 51.59], [-0.12, 51.505]]],
              [[[-0.09, 51.305], [-0.09, 51.39], [-0.12, 51.39], [-0.12, 51.305]]]
            ]
          }
        }]
      };
    return (
      <Map center={position} zoom={this.state.zoom} ref='map'>
        <TileLayer
          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'
        />
        <GeoJSON
            ref="geojson"
            data={geojsonObject}
            onEachFeature={this.onEachFeature}
          />
      </Map>
    );
  }
}

2 个答案:

答案 0 :(得分:1)

首先在渲染函数中定义地图:

   <LeafletMap
      ref='map'>
      <GeoJson
        ref="geojson"
        data={this.props.data}
        onEachFeature={this.onEachFeature.bind(this)}
      />
     </LeafletMap>

然后定义一个列出所有侦听器的onEachFeature函数

onEachFeature(feature, layer) {
    layer.on({
      mouseover: this.highlightFeature.bind(this),
      mouseout: this.resetHighlight.bind(this),
      click: this.clickToFeature.bind(this)
    });
  }

然后定义每个事件句柄函数,例如单击函数 或鼠标悬停

 clickToFeature(e) {
     var layer = e.target;
     console.log("I clicked on " + layer.feature.properties.name);

  }

没有看到Leaflet标签代码以及如何调用onEachFeature,我无法真正帮助您拥有ES6示例。 您可能不会像在JSFiddle示例中那样调用它

  onEachFeature={this.onEachFeature}

答案 1 :(得分:0)

对于ES6,您应该使用此代码:

onEachFeature = (feature, layer) => {
  layer.on({
    click: this.clickToFeature
  });
}

代替此:

onEachFeature = (feature, layer) => {
  layer.on({
    click: this.clickToFeature.bind(this)
  });
}