2011-11-06 166 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