2012-11-19 4 views
-1

지도에 여러 개의 마커를 표시하는 데 문제가 있습니다. 코드는 배열을 반복하면서 마커를 표시하고 정보 윈도우의 내용을 반환 된 주소로 설정하기 전에 위도, 경도 값을 역 지오 코딩합니다.Google지도의 여러 마커 : 마지막 마커 만 표시됩니다.

내 코드는 다음과 같습니다.

for(var i=0; i < useNowArray.length; i++) { 
     // plot useNow on map 
     latlng = new google.maps.LatLng(useNowArray[i]['lat'], useNowArray[i]['lon']); 
     console.log(useNowArray[i]['lat'] + useNowArray[i]['lon']); 
     geocoder.geocode({'latLng': latlng}, function(results, status) 
     { 
      if (status == google.maps.GeocoderStatus.OK) 
      { 
       if (results[0]) 
       { 
        marker = new google.maps.Marker({ 
         position: latlng, 
         map: map, 
         icon: 'http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png', 
         title: results[0].formatted_address 
        }); 
        content = results[0].formatted_address; 
        console.log(content); 

        google.maps.event.addListener(marker, 'click', (function(marker, i) { 
         return function() { 
          infowindow.setContent(content); 
          infowindow.open(map, this); 
         } 
        })(marker, i)); 
       } 
      } else { 
       alert("Geocoder failed due to: " + status); 
      } 
     }); // end geocode function 
    } 
    var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 

마지막 마커 만 표시되는 것이 문제입니다.

내가 무엇이 누락 되었습니까? :(

많은 감사.

답변

0

귀하의 마커 변수, 지오 코딩 비동기입니다. 루프는 useNowArray.length 요청을 발사, 실행, 각자가 결과 위치.와 글로벌 마커를 글로벌 업데이트되는 루프가 완료되면 마커는 루프의 마지막 위치에 남아있는 유사 질문이 문제에 대한 해결책으로 :.

+0

사실 그는이 코드를 사용하고 있습니다 : http://www.svennerberg.com/2012/03/adding-multiple-markers-to-google-maps-from-json/하지만 극히 예민한 예입니다. 즉, 작동하지 않습니다. 당신이 언급 한 이유. 나는 또한이 사람이 어떻게 책을 쓰고 출판업자를 찾을 수 있는지 궁금해. – Bytemain

+0

해당 [page] (http://www.svennerberg.com/2012/03/adding-multiple-markers-to-google-maps-from-json/)의 데모는이 문제가 없으므로 사용하지 않습니다. 지오 코더. – geocodezip

+0

답변 해 주셔서 감사합니다! 마커 변수를 로컬 변수로 변경해도 문제가 해결되지 않습니까? 나는 그것을했고 또한 fitBounds 예제와 같이 address_count와 배열 길이를 비교해 보았지만 나에게도 그다지 효과가 없었다. 도움이 T_T – chongzixin

관련 문제