Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leaflet React get map instance in functional component

I want to have a button outside the map that changes the view to another coordinates.

Is there any way to get mapContainer instance to call their functions? Or how can I implement that function?

I tried to get it by using ref, but it's not working. Here is my current code

const zoom = 13;

function Map({ regionCoord, regionName }) {

    const mapRef = useRef();

    function handleFlyToClick() {
      // This don't work
      // const map = mapRef.current.leafletElement 
      // map.flyTo(regionCoord, zoom)
    }

 return (   
        <React.Fragment>
            <Grid container >
                <Grid item xs={10}>
                    {regionCoord && <MapContainer
                        ref={mapRef}                     
                        center={[50,50]} 
                        zoom={zoom}                    
                        >
                        <TileLayer
                            attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                            url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
                        />            
   
                        <Marker position={regionCoord}>
                          <Popup>{regionName}</Popup>
                        </Marker>        
                    </MapContainer>}                               
                </Grid>
                <Grid item xs={2}>
                    <button onClick={handleFlyToClick}>Fly To</button>
                </Grid>
            </Grid>
        </React.Fragment>  
    )
    
}

export default Map

I'm using react-leaflet v3

like image 230
PauCasademont Avatar asked Dec 21 '20 14:12

PauCasademont


People also ask

What is react-leaflet?

From the React-Leaflet docs: React Leaflet provides bindings between React and Leaflet. It does not replace Leaflet, but leverages it to abstract Leaflet layers as React components. As such, it can behave differently from how other React components work. So, let’s go over how to build a Leaflet map component inside of a React application!

Can I add a Leaflet map to my react website?

Now, let’s say you have a React-based website ( check out this starter I set up for you on Stack Blitz ), and you want to add a Leaflet map to showcase your mapping skills. There is actually a React binding for Leaflet, React-Leaflet.

How to use markerpopup component in react leaflet?

We import the Popup component from the react-leaflet package. The MarkerPopup component just opens the popup when a marker is clicked on the map and it displays the venue name in the popup. I hope you have enjoyed my first medium article. In the next part we will cover the following:

What is mapcontainer in react?

The MapContainer component is responsible for creating the Leaflet Map instance and providing it to its child components, using a React Context. When creating a MapContainer element, its props are used as options to create the Map instance.


1 Answers

You need to use a component which will include your button inside. To take the map instance use whenCreated prop of MapContainer. I think mapRef is not valid anymore with the latest version.

MapContainer:

 const [map, setMap] = useState(null);

 <MapContainer
      center={[50, 50]}
      zoom={zoom}
      style={{ height: "90vh" }}
      whenCreated={setMap}
  >
...

</MapContainer>
<FlyToButton />  // use the button here outside of the MapContainer

....

Create the component with the button and its event

function FlyToButton() {
  const onClick = () => map.flyTo(regionCoord, zoom);
    
  return <button onClick={onClick}>Add marker on click</button>;
}

Demo

like image 151
kboul Avatar answered Sep 18 '22 09:09

kboul