Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 894201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:20:43+00:00 2026-05-15T14:20:43+00:00

I have a map and I’d like to change the marker image, when zoom

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:20:44+00:00Added an answer on May 15, 2026 at 2:20 pm

    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 markers array:

    var markers = [];
    

    And within your setMarkers() function, push each new marker into this array:

    markers.push(marker);
    

    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:

    function setMarkers(map, map_bounds, locations, iconLevel1, iconLevel2) {
      for (var i = 0; i < locations.length; i++) {
        var loc = locations[i];
        var myLatLng = new google.maps.LatLng(loc[1], loc[2]);
    
        var marker = new google.maps.Marker({
          position: myLatLng,
          map: map,
          icon: iconLevel1,    // iconLevel1 by default
          title: loc[0],
          zIndex: loc[3]
        });
    
        // Add custom properties to the marker object
        marker.iconLevel1 = iconLevel1;
        marker.iconLevel2 = iconLevel2;
    
        // Add the new marker to the markers array
        markers.push(marker);
    
        map_bounds.extend(myLatLng);
      }
    }
    

    Finally, it seems that you are already handling the zoom_changed event correct. First of all, I suggest checking if the zoomLevel has changed between 1 and 2, in order not to iterate through the markers array if there is no need. If there is a change, simply call the setIcon() method for each marker, and pass the custom property iconLevel1 or iconLevel2 depending on the zoomLevel:

    var zoomLevel = 1;
    
    google.maps.event.addListener(map, 'zoom_changed', function() {
      var i, prevZoomLevel;
    
      prevZoomLevel = zoomLevel;
    
      map.getZoom() < 5 ? zoomLevel = 1 : zoomLevel = 2;
    
      if (prevZoomLevel !== zoomLevel) {
        for (i = 0; i < markers.length; i++) {
          if (zoomLevel === 2) {
            markers[i].setIcon(markers[i].iconLevel2);
          }
          else {
            markers[i].setIcon(markers[i].iconLevel1);
          }
        }
      }
    });
    

    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:

    for (i = 0; i < markers.length; i++) {
      markers[i].setIcon(markers[i]['iconLevel' + zoomLevel]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have map on my site but I don't kno how to add marker
I have a resource called Book, then I have domains like: domain.com/books/272 But I
I have a map with custom marker images. The image I'm using is semi
I have to map a REST Webservice URL like http://server:8080/application/service/customer/v1 to createCustomer method in
I have a Map that uses a Set for the key type, like this:
I have map with 30-50 polygons on it. There are like 4000 points in
for example we have map map<char,int>mymap; mymap['a']=101; mymap['c']=45; mymap['b']=76; mymap['d']=98; i know that if
I have map and some views on it. I would like to make some
I have a map defined like this std::map<some_key_type, std::string::iterator> mIteratorMap; And a huge string
I have a map of a building and I'd like to programmatically highlight specific

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.