Skip to content
Advertisement

Ionic Angular Leaflet – performant rendering of many svg markers

I want to render many custom (svg) markers on my map. After my initial research I found a couple of approaches, but none of these seem to be applicable in my case. I’m using ionic/angular 5.0.0 and leaflet 1.7.1.

This is what I have:

JavaScript

I’d like to replace or customize the leaflet circleMarker with my svg or find a performant way to render a lot of svg elements in my map (thousands).

I know, I could use markers to display svg icons, however the performance of the application will suffer immensely, once you hit a few hundred markers.

I’d like to have the option to initialize the map like so:

JavaScript

or be able to use a custom renderer, like so:

JavaScript

That way, the markers will be drawn onto the canvas and not be treated as single DOM-Elements.

I tried to implement this solution, but I was unable to integrate it in my angular-typescript application properly.

I also looked at this question and installed and tested all the suggested libraries. However the question was too open and the libraries weren’t satisfying to me and seemed to only serve a minimalistic purpose. Maybe I’m just to dumb to integrate them properly (I don’t want to loose the benefits of angular and typescript, though)…

I feel like there has to be a simple solution here, but I cannot seem to find it. Am I missing something here?

Any help is greatly appreciated. Thanks!

Advertisement

Answer

Ok, so after many hours of trial and error, I eventually figured it out. I used and changed the code from several answers and examples to fit my specific use case. So if anyone is curious to what I did, here it goes…

I put all my code into one file for your convenience.

map.page.ts:

JavaScript

This is the result: enter image description here

This works for me, however I don’t know if there’s a more performant or better way of doing this. Anyway, I hope it helps.

EDIT

I realized that if you put the const imageBitmap = new Image(); outside the _displayCustomSVG() you could run into some inconsistencies with the drawImage function from leaflet.

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