Redrawing/Refreshing a Marker Icon

1,734 views
Skip to first unread message

Patrick Doody

unread,
Feb 5, 2013, 9:27:19 AM2/5/13
to leafl...@googlegroups.com
I am using ~200 or so Markers, each with different Icons that change on a very regular basis (up to every second or so). The icons very in color, style, and direction, and I am simply updating the "iconUrl" property in each Icon.

My problem is this: I see no way to force a redraw or refresh an icon for a Marker. The only way to update the icon of a Marker, from what I have seen, is to call Marker.setIcon(). This is what I currently do. It works. However, this is very non-performant and slow. Calls to setIcon() trigger calls to Marker._initIcon() and Marker.update(), which will remove the icon entirely, and then re-add the same icon again, re-initializing the entire icon each time.. In my context, this setIcon() call is becoming a weighty method when all I need to do is refresh the drawable state of my existing icon.

My question is; is there any way I can hack it so I can quickly change the iconUrl of my Icons, without having to completely remove/readd/update the Marker? This would increase my performance significantly.

Thanks,
Patrick

Sean Callan

unread,
Feb 5, 2013, 3:08:48 PM2/5/13
to leafl...@googlegroups.com
I'd also love to see an answer to this question.  I have a similar problem, though not of the same scale, and the blip that occurs after changing the `iconUrl` is very disruptive.  Would this better handled by the `L.DivIcon`?  There isn't much, if any, documentation/examples for the DivIcon so I'm not sure if this is a suitable use-case.

Thanks! 

bacci.g...@gmail.com

unread,
Feb 5, 2013, 5:57:05 PM2/5/13
to leafl...@googlegroups.com


Il giorno martedì 5 febbraio 2013 15:27:19 UTC+1, Patrick Doody ha scritto:

My question is; is there any way I can hack it so I can quickly change the iconUrl of my Icons, without having to completely remove/readd/update the Marker? This would increase my performance significantly.

 
try with _icon.src properties of the marker
i.e. activeMarker._icon.src = 'some/other/image.png'

Paulo Vieira

unread,
Feb 5, 2013, 9:16:36 PM2/5/13
to leafl...@googlegroups.com
On Tue, Feb 5, 2013 at 10:57 PM, <bacci.g...@gmail.com> wrote:


My question is; is there any way I can hack it so I can quickly change the iconUrl of my Icons, without having to completely remove/readd/update the Marker? This would increase my performance significantly.

 
try with _icon.src properties of the marker
i.e. activeMarker._icon.src = 'some/other/image.png'

Yes, it's a bit "hacky" but should be more efficient.

Here's an example which I did some months ago while implementing a hover event for markers:

http://jsfiddle.net/RvRPh/2/

Message has been deleted

Sean Callan

unread,
Feb 6, 2013, 3:30:36 PM2/6/13
to leafl...@googlegroups.com
Thanks so much Paulo, that worked like a charm! 
Reply all
Reply to author
Forward
0 new messages