简体   繁体   中英

Error when trying to map a choropleth using react-leaflet-choropleth: Cannot read property 'map' of undefined

I am attempting to make a choropleth in react using react-leaflet-choropleth .

I have leaflet working fine.
Now when I try to load a geojson (crimes_by_district) using the library I am getting an error when mapping through the features, specifically:

Cannot read property 'map' of undefined

It seems I'm not accurately referencing the right array to map.

I have looked at the issues in the git repo and tried the suggestions with no luck. I am wondering if perhaps I am referencing the incorrect values from the geoJson I am using.

Below is my code:

Leaf.js (rendered in App.js)

import React, { Component } from 'react';
import { Map, TileLayer } from 'react-leaflet';
import classes from './leaf.module.css'
import Choropleth from 'react-leaflet-choropleth'
import geojson from "./assets/crimes_by_district.geojson";
    
const style = {
   fillColor: '#F28F3B',
   weight: 2,
   opacity: 1,
   color: 'white',
   dashArray: '3',
   fillOpacity: 0.5
};
    
class Leaf extends Component {
   render() { 
      return (         
         <Map>
           <Choropleth
             data= {{type: 'FeatureCollection', features: geojson.features}}
             valueProperty={(feature) => feature.properties.value}
             // visible={(feature) => feature.id !== active.id}
             scale={['#b3cde0', '#011f4b']}
             steps={7}
             mode='e'
             style={style}
             onEachFeature={
               (feature, layer) => layer.bindPopup(feature.properties.label)
             }
             ref={(el) => this.choropleth = el.leafletElement}
           />
         </Map>
      );
   }
}
    
export default Leaf;

Create your own choropleth wrapper using the same choropleth library similar to the extension you include, as react-leaflet-choropleth seems to be outdated:

function Choropleth() {
  const { map } = useLeaflet();

  useEffect(() => {
    fetch(
      "https://raw.githubusercontent.com/timwis/leaflet-choropleth/gh-pages/examples/basic/crimes_by_district.geojson"
    )
      .then((response) => response.json())
      .then((geojson) => {
        L.choropleth(geojson, {
          valueProperty: "incidents", // which property in the features to use
          scale: ["white", "red"], // chroma.js scale - include as many as you like
          steps: 5, // number of breaks or steps in range
          mode: "q", // q for quantile, e for equidistant, k for k-means
          style,
          onEachFeature: function (feature, layer) {
            layer.bindPopup(
              "District " +
                feature.properties.dist_num +
                "<br>" +
                feature.properties.incidents.toLocaleString() +
                " incidents"
            );
          }
        }).addTo(map);
      });
  }, []);

  return null;
}

and then import it as a Map child:

<Map center={position} zoom={11} style={{ height: "100vh" }}>
        <TileLayer
          attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        />
   <Choropleth />
</Map>

you can extend it even further by passing various variables you need as props.

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM