弹出反应单张图库

问题描述 投票:0回答:1

我在我的react应用程序中使用react-leaftlet地图库https://react-leaflet.js.org/en/,并且我已经在地图上绘制了一些标记,并且当用户单击标记时,会出现一个弹出窗口。当用户也单击我的地图区域时,我想打开一个类似的弹出窗口。我怎样才能做到这一点?以下是我的代码,用于使用弹出窗口渲染标记。 (地图使用geojson数据渲染)

markerHospitalRender() {
    return this.props.hospitalData.map(item => {
      const position = [item.district_lat, item.district_long];
      return (
        <Marker position={position} icon={this.grenIcon}>
          <Popup>
            <span style={{ display: "block" }}>{item.name}</span>
          </Popup>
        </Marker>
      );
    });
  }

<Map
   className="map"
   center={center}
>
   <GeoJSON
      data={geo}
      style={this.hospital_style}
   />
   {this.markerHospitalRender()}
</Map>
javascript reactjs leaflet geojson react-leaflet
1个回答
1
投票

使用onEachFeaturereact-leaflet包装器上的GeoJSON道具来传递箭头功能,类似于本机的传单onEachFeature function,以便在每个区域上单击时都可以生成弹出窗口。

<GeoJSON
      data={geo}
      style={this.hospital_style}
      onEachFeature={onEachFeature}
   />

const onEachFeature = (feature, layer) => {
  const popupContent = `District Code: ${feature.properties.electoralDistrictCode} <br> District: ${feature.properties.electoralDistrict}`;
  if (feature.properties && feature.properties.popupContent) {
    popupContent += feature.properties.popupContent;
  }
  layer.bindPopup(popupContent);
};
© www.soinside.com 2019 - 2024. All rights reserved.