CoffeeScript循环创建单击函数侦听器会创建多个侦听器,但具有相同的参数

时间:2016-07-16 22:21:11

标签: google-maps google-maps-api-3 coffeescript

我有一个for循环,可以自动生成标记和相应的单击侦听器。但是,所有单击函数似乎都与循环中的最后一个值具有相同的值。

我怎样才能解决这个问题。示例代码在这里;

  layMarkers = ->
    $.ajax '/locations',
        type: 'GET'
        dataType: 'json'
        error: (jqXHR, textStatus, errorThrown) ->
            console.log errorThrown
        success: (data, textStatus, jqXHR) ->
            buses = data["locations"]
            for i in buses
              marker = new google.maps.Marker(position: new google.maps.LatLng(i["latitude"], i["longitude"]),
              map: map
              title: "#{i["number_plate"]}, #{i["speed"]}km/h"
              animation: google.maps.Animation.DROP)
              marker.setMap(map)
              marker.addListener 'click', ->
                showDirection(i["terminal_number"], i["bus_number"], i["number_plate"],i["speed"] )
                # infowindow("#{i["number_plate"]}, #{i["speed"]}km/h, Terminal: #{i["terminal_number"]}, Bus Number: #{i["bus_number"]}").open map, marker
                return

传递给showDirection的值是迭代的数组中的最后一项。我能俯瞰什么?

0 个答案:

没有答案