Google Maps Api Marker setOptions()更改图标

时间:2012-12-17 14:49:23

标签: javascript google-maps-api-3

我想在绘制多边形选中时更改标记图标。在addMarker()中,标记从JSON数据中解析并推送到allMarkers数组中。未选中时图标为红色,选中后图标将变为白色。

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"
                });
            }
        }
    }   

}

selectMarkersInPoly()有什么问题???谢谢您的帮助?

1 个答案:

答案 0 :(得分:6)

这不是javascript匿名对象的正确语法:

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

(我认为你会在javascript控制台中出现javascript错误) 这应该有效:

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