异步调用在Coffee Script中不起作用

时间:2012-06-09 17:47:09

标签: google-maps coffeescript

我正在使用CoffeeScript,Backbone.js和Google Maps API来反转地理编码lat / lng

我有功能

  country: (origin, callback) ->
    @geocoder = new google.maps.Geocoder
    @geocoder.geocode(
        'latLng': origin,
        (results, status) => 
            if status is google.maps.GeocoderStatus.OK
              callback(result[6])
            else alert("Geocode was not successful for the following reason: " + status);
    )

当我打电话给我时,我使用:

@country(origin, (data) =>
   console.log(data.formatted_address)
)

顺便说一句:

origin = new google.maps.LatLng(origin_lat, origin_lng)

这不起作用,它似乎甚至没有调用它。我在(data)下有回调功能,但无法让它工作......

由于

1 个答案:

答案 0 :(得分:0)

不确定问题的整体背景,但无论如何我都会采取措施。

您定义国家/地区功能的方式通常是对象或地图的一部分。如果您只想要一个独立的功能,请尝试:

country = (origin, callback) ->
   ...

然后在调用它时,尝试将@country(...)替换为country(...)

根据评论,您对地理编码的调用也看起来不对。您需要创建GeocoderRequest的实例并传递它而不是'latLng': origin对。不幸的是,我没有太多使用Google风格的API,所以如果这是你真正的问题我对你没用多少。