2011-11-06 171 views
1

我有一個CoffeeScript的類:呼叫方法

class window.MapHandler 
    map = null 

    makeMap:() -> 
     latlng = new google.maps.LatLng(54.711929,20.5089); 
     myOptions = 
      zoom: 12 
      center: latlng 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     map = new google.maps.Map(document.getElementById("map_canvas"), myOptions) 
     @geocode("Калининград, Чернышевского 101") 


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

    geocode: (address) -> 
     geocoder = new google.maps.Geocoder 
     geocoder.geocode(
      'address': address, 
      (results, status) -> 
       if status is google.maps.GeocoderStatus.OK 
        map.setCenter(results[0].geometry.location) 
        @placeMarker(results[0].geometry.location) 
       else alert("Geocode was not successful for the following reason: " + status); 
     ) 

有一個問題,當我打電話placeMarker從匿名功能的方法從地址解析方法:visualizer.js:37Uncaught類型錯誤:對象[對象DOMWindow]沒有方法'placeMarker'

我該如何調用這個方法?

回答

4
geocode: (address) -> 
    geocoder = new google.maps.Geocoder 
    geocoder.geocode(
     'address': address, 
     (results, status) => 
      if status is google.maps.GeocoderStatus.OK 
       map.setCenter(results[0].geometry.location) 
       @placeMarker(results[0].geometry.location) 
      else alert("Geocode was not successful for the following reason: " + status); 
    ) 

注意第5行中的脂肪箭頭 - 它保留this@瓶蓋內。

+0

是的,thx。我最近自己做了。 – rkotov93