2011-11-11 6 views
1

나는 왜 모두의 끝에서 클래스커피 스크립트 바인딩 변수

class window.MapHandler 
    map = null 
    userLocationMarker = null 

    makeMap: (location) -> 
    myOptions = 
     zoom: 14 
     center: location 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    @map = new google.maps.Map(document.getElementById("map_canvas"), myOptions) 
    placeMarker: (location, icon_path) -> 
    if icon_path 
     markerImage = new google.maps.MarkerImage(icon_path, null, null, null, new google.maps.Size(25, 25)) 
    else 
     markerImage = null 
    marker = new google.maps.Marker(
     position: location 
     map: @map 
     icon: markerImage) 

    defineUserLocation:() -> 
    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 
      content: 'Если это не ваше местоположение - передвиньте маркер' 
     ) 
      @map.setCenter(pos) 
      @userLocationMarker = @placeMarker(pos, null) 
    ) 
    alert @userLocationMarker.getPosition() 

이 내가 가지고 중심으로지도와 메이커이 시점에서, 그러나 @userLocationMarker입니다 정의되지에는 getPosition 메소드 호출 오류?

답변

0

navigator.geolocation.getCurrentPosition은 비동기 함수입니다. 해당 콜백 (@userLocationMarker 설정)은 alert 호출 이후에 실행됩니다. 예 : alert 행을 콜백에 넣으십시오.

+0

그러나 나는 userLocationMarker = null을 클래스 최상위에 가지고 있습니다. – rkotov93

+0

@ koshak1993 : 아, userLocationMarker : null이어야합니다. – thejh

+0

이해가 안됩니다. 왜, (위치) 함수가 변수 맵을 보지만, userLocationMarker가 보이지 않는다. – rkotov93