Leaflet: Add image overlay with a GeoJSON mask

Viewed 967

I'm trying to add an image overlay on top a Leaflet map, but I want it to be drawn only in the area that a geo-json multipolygon closes (so treat the geo-json layer as a mask for the image). The image I want to display is rectangular, with no transparent areas.

By using a SVG mask, I achieve my purpose. As the geo-json is put in a SVG element, I'm able to fill it with my image.

https://gyazo.com/069ecec5904026d5f4d0624c6f1a2a7e

But the problem arises when I start zooming in. The SVG where the geo-json is placed changes its size and shape. It becomes smaller than it should be (because areas that are outside the map are removed from the SVG), so the image is not centered on its exact position anymore.

https://gyazo.com/68148043d5ff3d11c0a13aa1d29c197b

As you can see, both when zooming and when panning the map, the image moves, and that's because the SVG where the geo-json is displayed changes its width and height.

Here is a fiddle with the code I came up at the moment: https://jsfiddle.net/8n3m6t51/


I can think for several solutions:

  • Force Leaflet to always draw the full geo-json area, even those polygons or areas that are out of the screen.
  • Use a ImageOverlay and set it a mask (but I think this one would be impossible).

But at the moment I haven't figured out what to do to get this issue fixed.

0 Answers
Related