Skip to content
Advertisement

How to get borders for the react-leaflet map and check markers inside the map?

My code is here:

JavaScript

My question is, how can I use bbox to check if some of my markers is inside the map? When I try to apply:

JavaScript

or this:

JavaScript

I faced the error: bbox is not defined

I don’t know how to return bbox from function LocationMarker(). I would be grateful for any help. Thank you.

Advertisement

Answer

You need to follow a slight different approach then:

Declare bbox variable on the parent component(App) and store the instance. You will need it to be able to use contains method later. you can pass bbox and setBbox as props on LocationMarker comp. Doing that you will have the communication between the two components.

Also move the LocationMarker comp outside the App.

Here is the LcoationMarker component:

JavaScript

Here is the App component. You can use bbox isntance in this example via a button. Make sure to check that bbox is defined before using it.

JavaScript

Here is a demo with all the pieces together

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement