2011-11-11 134 views
1

我有一個類CoffeScript綁定變量

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