2013-07-29 8 views
0

다음 코드는지도를 렌더링하지만 click 이벤트에서 콜백이 실행되지 않습니다. 어떤 아이디어?Google지도 API - 클릭 이벤트가 작동하지 않습니다.

google.maps.visualRefresh = true 
url = 'http://localhost:3000/closeby' 

getCloseBy = (pos) -> 
    $.post url, {'center': pos} 

initialize = -> 
    mapOptions = 
     zoom: 15 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    map = new google.maps.Map document.getElementById('map-canvas'), mapOptions 

    if navigator.geolocation 
    navigator.geolocation.getCurrentPosition (position)-> 
     pos = new google.maps.LatLng position.coords.latitude, position.coords.longitude 
     infowindow = new google.maps.InfoWindow 
      map: map 
      position: pos 
     map.setCenter pos 
    , -> 
     handleNoGeolocation true   
    else 
    handleNoGeolocation false  

    handleNoGeolocation = (errorFlag) -> 
     if errorFlag 
      content = 'Geolocation failed' 
     else 
      content = 'Your browser does not support Geolocation' 
     options = 
      map: map 
      position: new google.maps.LatLng 60, 105 
      content: content 
     infowindow = new google.maps.InfoWindow options 
     map.setCenter options.position 

    placeMarker = (location) -> 
    marker = new google.maps.Marker 
     position: location 
     map: map 

    google.maps.event.addListener map, 'click', (event)-> 
    placeMarker event.latLng  

google.maps.event.addDomListener window, 'load', initialize 

답변

0

범위 지정 문제와 비슷합니다. placeMarkerinitialize 외부에 있으며 map이 정의되어 있습니다. initialize 함수 안에서 placeMarker을 움직여보십시오. 또는 map을 매개 변수로 placeMarker에 전달하십시오. 나는 커피 스크립트를 사용하지 않는,하지만이 작동합니다 initialize (그리고이 map 전달)의 내부 렌더링 :

업데이트

 var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 

     google.maps.event.addListener(map, 'click', function(event) { 
      placeMarker(event.latLng); 
     }); 

     var placeMarker = function (location) { 
      var options = { position: location, map: map }; 
      var marker = new google.maps.Marker(options); 
     }; 
+0

내가 코드를 업데이트 및 장소 표시에 대한 인수로 맵을 통과하지만, 내가 클릭하는 곳에 마커가 표시되지 않습니다. – tldr

+0

답변이 업데이트되었습니다. 보시다시피 저는 CoffeeScript를 사용하지 않습니다. 어쩌면 방법을 이동하는 것은 갈 길입니다. – michaelrp

+0

코드를 업데이트하고 초기화 코드를 넣어 두었습니다. 여전히 작동하지 않습니다. – tldr

관련 문제