Google Maps Api Marker setOptions() change icon

11,716

This is not the correct syntax for a javascript anonymous object:

allMarkers[j].setOptions({
                icon = "http://labs.google.com/ridefinder/images/mm_20_white.png"
            });

(I would think you would get javascript errors in the javascript console) This should work:

allMarkers[j].setOptions({
                icon: "http://labs.google.com/ridefinder/images/mm_20_white.png"
            });
Share:
11,716
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I want to change a marker icon when selected by a drawed polygon. In addMarker() the markers are parsed from JSON data and push into allMarkers array. The icon is red when not selected and would become white when selected.

    function addMarker(lat,lng,i){
        var myLatlng = new google.maps.LatLng(lat,lng);
        var marker = new google.maps.Marker({
            position: myLatlng,
            icon: "http://labs.google.com/ridefinder/images/mm_20_red.png",
            map: map
        });
        marker.shapeId = '0';
        allMarkers.push(marker);    
    }   
    
    
    function selectMarkersInPoly() {
        alert(allMarkers.length)
        for (var i=0; i < createdShapes.length; i++) {
            for (var j=0; j < allMarkers.length; j++){
                var latlong = allMarkers[j].getPosition();
                if(google.maps.geometry.poly.containsLocation(latlong, createdShapes[i]) == true) {
                    allMarkers[j].shapeId = createdShapes[i].id;
                    allMarkers[j].setOptions({
                        icon : "http://labs.google.com/ridefinder/images/mm_20_white.png"
                    });
                }
            }
        }   
    
    }
    

    What is wrong with selectMarkersInPoly() ??? Thank you for your help?