0

내가 뭘하려고이 (how to set a zoom level using Gmap4rails)
유사하지만, 대신 뭔가 :gmaps4rails V2 다각형 클릭 이벤트

google.maps.event.addListenerOnce(Gmaps.map.getMapObject(), 'idle', function(){} 

내가 그래서

google.maps.event.addListenerOnce(polygon, 'click', function(){} 

같은 _
을 원하는 나는 이것을 실험으로 시도했다 :

handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){ 
     polygon = handler.addPolygons(<%= raw @myhash.to_json %>); 
} 
google.maps.event.addListener(polygon, "click", function(evt) { 
     alert("hello!"); 
}); 

하지만 작동하지 않습니다 ....

내 질문은, 어떻게 다각형에 대한 수신기를 추가 할 것이라고?

답변

0

IT는 gmaps4rails 프록시 객체 내에 살고 변수 범위와 실제 구글 개체의 문제 :

handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){ 
    var polygons = handler.addPolygons(<%= raw @myhash.to_json %>); 

    for (var i=0;i < polygons.length; i++){ 
    var polygon = polygons[i]; 
    google.maps.event.addListener(polygon.getServiceObject(), "click", function(evt) { 
     alert("hello!"); 
    }); 
    } 
} 
+0

이 작품, 고마워요! – ralphie02

1

도큐멘트에 따르면 google.maps.event.addListenergoogle.maps.event.addListenerOnce은 첫 번째 인수로 단일 개체 만 허용하므로 markers 배열은 작동하지 않습니다. 각 마커마다 addListener 번씩 전화해야합니다. 대략적으로 좋아하는 것 :

handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){ 
    markers = handler.addMarkers(<%= raw @myhash.to_json %>); 

    for (marker in markers) { 
     google.maps.event.addListener(marker, "click", function(evt) { 
      alert("hello!"); 
     }); 
    } 
} 

괜찮습니다.

+0

이 실제로 혼란을 드려 죄송 내 다각형의 리스너를 갖고 싶어. 내 질문을 편집 할 것입니다 .... – ralphie02

+0

나는 질문을 편집했고, 당신이 대답을 알고 있거나 생각이 있다면, 지금 더 명확 해지기를 바랍니다. 제게 알려주십시오. 감사합니다 – ralphie02