DEV Community

Rafael Lage
Rafael Lage

Posted on

Using react-leaflet to generate one tiled layer

The problem: Leaflet does not support single tile layer.

(This was posted in 2017, on medium)

Which means that if you need to render some dynamic cluster, generated by geoserver, like this:leaflet-1

Leaflet will, instead, show something like this:leaflet-2

Which is messed up. Fortunately, we (me and Arlindo Pereira, at MPRJ) got a workaround using ImageOverlay element from Leaflet, which is pretty simple, but it took a lot of time to discover.

React-leaflet is a lib that exposes components to work with leaflet. So it exposes react components like Map, TileLayer, WMSTileLayer, and so on. It also exposes ImageOverlay. To get this to work, you need to use ImageOverlay component like this:

Enter fullscreen mode Exit fullscreen mode

Where imageBounds needs to be a Leaflet Bounds object, which should be the bounds for the current visible portion of the map, like this:

Enter fullscreen mode Exit fullscreen mode

The imageURL parameter is the URL to the service that will generate the image and should be like this:

let imageURL = `${ENDPOINT}?
Enter fullscreen mode Exit fullscreen mode

This is a WMS call. The main things you should note are the Height, Width and Bbox attributes. The height and width need to be in pixels and related to the size of the map in your app, so to speak. Our map fits the html document, so we got it like this:

let clientHeight = document.body.clientHeight
let clientWidth = document.body.clientWidth
Enter fullscreen mode Exit fullscreen mode

And the Bbox is the same coordinate as mentioned before.
You can see how it was before:


And how it is now:


Or here, live:
Here to see the code:

InLoco is aGeographic Information System (GIS) used by Ministério Público do Estado do Rio de Janeiro to show social, institutional and administrative data, made with React and Leaflet, interacting with a public GeoServer backend. You are welcome to send you PR =)

Top comments (0)