Skip to content Skip to sidebar Skip to footer

How Do I Access All My Google Maps Markers

I am not seeing it i'll think if one of you looks at it can immediately tell me what i have to do. Now i resize my marker but it only takes the last marker of the array. here the c

Solution 1:

The problem is this: you're looping over all your markers, adding a new event listener for the map's zoom_changed event. In each of those event listeners, you're referring to the variable marker. This event listener function doesn't get executed at the moment you define it, it only happens when the zoom changes obviously. So at that point, the variable marker will equal whatever it was at the very last iteration of your for loop.

Instead you need to change how you setup this event listener, something like this:

for (i = 0; i < locations.length; i++) {
        var myLatLng = {lat: locations[i][0].lat, lng: locations[i][0].lng};

    marker = new google.maps.Marker({
        position: myLatLng,
        icon: icon1,
        map: map
    });

    setMarkerSize(marker);
}

functionsetMarkerSize(marker) {
    var icon = marker.icon;
    map.addListener('zoom_changed', function() {
        if (map.getZoom() === 16) {
            icon.scaledSize = new google.maps.Size(15, 15);
            icon.size = new google.maps.Size(15, 15);
            marker.setIcon(icon);
            console.log(marker.icon.size);
        }
        if (map.getZoom() === 17) {
            icon.scaledSize = new google.maps.Size(32, 32);
            icon.size = new google.maps.Size(32, 32);
            marker.setIcon(icon);
            console.log(marker.icon.size);
        }
        if (map.getZoom() === 18) {
            icon.scaledSize = new google.maps.Size(90, 90);
            icon.size = new google.maps.Size(90, 90);
            marker.setIcon(icon);
            console.log(marker.icon.size);
        }
    });
}

In this case, marker inside the setMarkerSize function is a local variable that will be different each time you call the function.

Post a Comment for "How Do I Access All My Google Maps Markers"