I have a map and I’d like to change the marker image, when zoom is greater then 5.
I know how to detect zoom change, but I don’t see how to change the image.
I have a map and I’d like to change the marker image, when zoom
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That should be quite easy. I gave a look to your code, and it looks like you are not keeping a reference to your markers. This is the first thing that you should do.
Therefore create a
markersarray:And within your
setMarkers()function, push each new marker into this array:Now you will be able to iterate over your markers with a for loop:
for (i = 0; i < markers.length; i++).Ideally, we should also store the two icons of each marker in the marker object itself. JavaScript objects can be augmented with custom properties very easily. To do so, you may want to change your
setMarkers()function as follows:Finally, it seems that you are already handling the
zoom_changedevent correct. First of all, I suggest checking if thezoomLevelhas changed between1and2, in order not to iterate through themarkersarray if there is no need. If there is a change, simply call thesetIcon()method for each marker, and pass the custom propertyiconLevel1oriconLevel2depending on the zoomLevel:The above should work, but you may want to refactor your for loop as follows, using the subscript notation instead of the dot notation to access the properties of the markers: