2015-01-16 3 views
0

Meteor 용 Meteorj 및 dburles google지도 패키지 사용.Meteor - Google지도 InfoWindow 이벤트 실행 안 함

목표 : 마커를 캔버스에 맵핑 한 다음 onclick을 사용하여 정보 창을 표시합니다. 창문을 공개하기 위해 Google지도 이벤트를 실행하는 데 문제가 있습니다.

내 코드 :

Template.myMap.helpers({ 
    mapOptions: function() { 
    var locations = [ 
     ['Kroger', 34.069201, -84.231052, 5], 
     ['Fresh Produce', 34.069802, -84.234164, 4], 
     ['Starbucks', 34.069003, -84.236323, 3], 
     ['Mall of Georgia', 34.069204, -84.232016, 2], 
     ['Avalanche', 34.069705, -84.238207, 1] 
     ] 

// Make sure the maps API has loaded 
if (GoogleMaps.loaded()) { 
    // We can use the `ready` callback to interact with the map API once the map is ready. 
    GoogleMaps.ready('myMap', function(map) { 
    var infowindow = new google.maps.InfoWindow(), 
     marker, i; 

    for (i = 0; i < locations.length; i++) { 
     marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
     map: map.instance 
     }); 

     google.maps.event.addListener(marker, 'click', function() { 
     return function() { 
      infowindow.setContent(locations[i][0]); 
      infowindow.open(map, marker); 
     } 
     }); 
    } 
    }); 
    return { 
    center: new google.maps.LatLng(34.069705, -84.238), 
    zoom: 16 
    }; 
} 
} 

});

답변

0

명확하지 않은 이유 때문에 이벤트 처리보다는 이벤트 처리기에서 함수를 반환합니다. 이 시도 :

google.maps.event.addListener(marker, 'click', function() { 
    infowindow.setContent(locations[i][0]); 
    infowindow.open(map, marker); 
}); 

UPDATE

다른 문제는 당신이 Google지도 맵 오브젝트가 아닌 인스턴스 자체에 추가하려는 것입니다. 난 당신의 코드를 실행하지 않은, 또한

infowindow.open(map.instance, marker); 

,하지만 난 i의 값으로되지 않습니다, 당신은 당신이 구조 한 방법으로 문제로 실행하는 것 같아요 : 그것은해야한다 핸들러가 실제로 실행될 때 의도된다. forEach 대신 밑줄을 사용하여 각 처리기가 자체 폐쇄로 선언되도록하는 것이 좋습니다.

_.forEach(locations, function(location) { 
     var marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(location[1], location[2]), 
      map: map.instance 
      }); 

     google.maps.event.addListener(marker, 'click', function() { 
      infowindow.setContent(location[0]); 
      console.log(map, marker, infowindow); 
      infowindow.open(map.instance, marker); 
     });   
    }); 
+0

내가이 작업을 수행 할 때 "정의되지 않은"infowindowviewpool 속성을 읽을 수 없습니다. "라는 오류 메시지가 나타납니다. 너는 어떤 생각을 가지고 있니? 난 그것이 funtion에 오기 전에지도가 이미 정의되고 있다고 생각하고 그렇지 않은 경우에만 오류를 가져와야합니다. –