关闭标记infowindow并点击更改标记颜色

时间:2015-01-22 01:27:15

标签: javascript google-maps google-maps-api-3

您好我已经在stackoverflow上尝试了很多样本​​,不知道我哪里出错了。我有一个地图,其中有一些标记可以正常工作(更改颜色和打开信息问题是我希望以前的标记在选择新标记时更改回原始颜色关闭信息。不确定我在哪里出错。

<script type="text/javascript">             
        $(document).ready(function(){
            var i=0;
            var currentInfoWin = null;
            var currentMarker = null;
            var mapOptions = {
                mapTypeId:google.maps.MapTypeId.ROADMAP
            };
            var map = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
            $.getJSON('map.php',function(data){
                if(data.success == true) {
                    if(data.CData.length > 0){
                        $.each(data.CData,function(index, d){
                        addMarker(currentMarker, currentInfoWin, map, d.Latitude, d.Longitude);
                        });
                    }
                }           
            });     
        });

        function addMarker(currentMarker, currentInfoWin, map, Active, Lat, Lng){
            if (Lat != null && Lng != null){
                var LatLng = new google.maps.LatLng(Lat, Lng);                                      
                var marker = new google.maps.Marker({
                    position: LatLng,
                    map: map,
                    icon:{
                        path: google.maps.SymbolPath.CIRCLE,
                        scale: 5.0,
                        fillColor: "#F00",
                        fillOpacity: 0.7,
                        strokeWeight: 0.4
                    },
                });

                var content = '<div class="">test</div>';

                var infowindow = new google.maps.InfoWindow({ 
                    content: content,
                    maxWidth: 370
                });

                google.maps.event.addListener(marker, 'click', function (){
                    resetInfoWindow(currentInfoWin,currentMarker);
                    marker.setIcon({
                        path: google.maps.SymbolPath.CIRCLE,
                        scale: 5,
                        fillColor: "#00F",
                        fillOpacity: 0.7,
                        strokeWeight: 0.4
                    });
                    infowindow.open(map, marker);
                    currentInfoWin = infowindow;
                    currentMarker = marker;
                });             
            }               
        }

        function resetInfoWindow(currentInfoWin,currentMarker){
            if(currentInfoWin!==null){
                currentInfoWin.close();
                currentMarker.setIcon({
                    path: google.maps.SymbolPath.CIRCLE,
                    scale: 5,
                    fillColor: "#F00",
                    fillOpacity: 0.7,
                    strokeWeight: 0.4
                });
            }
        }

    </script>

1 个答案:

答案 0 :(得分:1)

所以我想通了:我把标记和infowindows放在一个数组中,在打开和设置新标记之前使用了一个循环点击重置所有标记...还删除了resetInfoWindow函数。不确定这是否是最有效的方式请进入。再次感谢!

google.maps.event.addListener(marker, 'click', function (){
    for (var i = 0; i < MarkerArray.length; i++) {
         MarkerArray[i].setIcon({
             path: google.maps.SymbolPath.CIRCLE,
             scale: 5,
             fillColor: "#F00",
             fillOpacity: 0.7,
             strokeWeight: 0.4
         });
      InfoWindowArray[i].close();
    }
    marker.setIcon({
        path: google.maps.SymbolPath.CIRCLE,
        scale: 5,
        fillColor: "#00F",
        fillOpacity: 0.7,
        strokeWeight: 0.4
    });
    infowindow.open(map, marker);
});