Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

reactjs - Using react-leaflet-geosearch in React Leaflet: cannot read property addControl

Trying to add react-leaflet-geosearch to my leaflet map and I get the error:

Cannot read property 'addControl' of undefined

I do not know what the problem seems to be:

My code:

render() {
    const prov = OpenStreetMapProvider();
    const GeoSearchControlElement = SearchControl;
    return (
      <>
       
         
          <MapContainer
            style={{ height: "100%", width: "100%" }}
            center={position}
            zoom="0"
            scrollWheelZoom={true}
          >
            <TileLayer
              attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
              url="https://api.mapbox.com/styles/v1/<style>/tiles/256/{z}/{x}/{y}@2x?access_token=<token>"
            />
            <Marker position={position} ></Marker>
            <ChangeMapView coords={position} />
            <GeoSearchControlElement
              provider={prov}
              showMarker={true}
              showPopup={false}
              popupFormat={({ query, result }) => result.label}
              maxMarkers={3}
              retainZoomLevel={false}
              animateZoom={true}
              autoClose={false}
              searchLabel={"Enter address, please"}
              keepResult={true}
            />
          </MapContainer>
        
      </>
    );
question from:https://stackoverflow.com/questions/65909171/using-react-leaflet-geosearch-in-react-leaflet-cannot-read-property-addcontrol

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You seem to use React Leaflet version 3 since you have a <MapContainer> component.

Unfortunately, the react-leaflet-geosearch plugin you are trying to integrate was made for React Leaflet version 2: https://github.com/TA-Geoforce/react-leaflet-geosearch/blob/master/package.json#L108

Due to the big API changes between these major versions, it is unlikely it will be compatible.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...