2012-05-04 2 views
1

나는 원격으로 좌표를 잡고지도에 놓고 폴리선과 연결하는 클릭 이벤트를 가지고있다. 같은 작업을하는 다른 클릭 이벤트가 있지만 별도의 레이어로 유지하여 사용자가 수행 한 클릭 각각을 전환 할 수 있습니다. 누구든지이 방법이나 다른 방법을 사용하기 위해 개별적으로 그룹화하는 좋은 방법을 알고 있습니까? - 좋은 시작이다Google지도에서 여러 개의 오버레이를 전환하는 방법은 무엇입니까?

drawPoints: function (points, color) { 
    $.each(points, function (key, val) { 
     var location = new google.maps.LatLng(val.Latitude, val.Longitude); 
     MyTest.addMarker(location, val.Name, val.Description); 
     MyTest.coordinates.push(location); 
    }); 

    this.path = new google.maps.Polyline({ 
     path: MyATest.coordinates, 
     strokeColor: color, 
     strokeOpacity: 1.0, 
     strokeWeight: 2 
    }); 

    this.path.setMap(this.googlemap); 
}, 

addMarker: function (location, title, html) { 
    var marker = new google.maps.Marker({ 
     position: location, 
     map: this.googlemap, 
     title: title, 
     html: html 
    }); 

    this.markers.push(marker); 
}, 

// Removes the overlays from the map, but keeps them in the array 
clearOverlays: function() { 
    if (this.markers) { 
     for (i in this.markers) { 
      this.markers[i].setMap(null); 
     } 
    } 
}, 

// Shows any overlays currently in the array 
showOverlays: function() { 
    if (this.markers) { 
     for (i in this.markers) { 
      this.markers[i].setMap(map); 
     } 
    } 
}, 
+1

안녕하세요 bruv가 시작으로이 피들을 가져 와서 여기에 그것을 복제하십시오 : http://jsfiddle.net/fHsAY/ :)) 좋은 것을 가지고! 치어 리더! –

답변

1

은 당신이 그들을 this.markers에 보관있어했습니다

여기에 지금까지 내 코드입니다. 이제 당신이해야 할 모든 각 마커에 대한 click 리스너 함수를 정의한다 :

for(i in this.markers) { 
    google.maps.addListener(this.markers[i], function() { 
     this.markers[i].clicked = true; }); 
} 

을 그리고, 사용자가 (당신이하고있는 것처럼) 마커를 통해서만 그들이 클릭 한 마커, 루프를 전환하려 할 때마다 그리고 this.markers [i] .clicked === 정의되지 않은 곳에 setMap을 null로 설정하십시오.

+2

자바 스크립트에서'for for' 루프를 사용하여 배열을 반복하지 마십시오. –

+0

동의 함, OP가 배열을 사용하고 있다는 사실에주의를 기울이지 않았다. 나는 당신이 drawPoints 메서드를 사용하고있는 $ .each() 도우미 반복자를 사용한다. – Adam

관련 문제