0

I have a for loop that automatically generates markers and the respective click listeners. However, all click functions seems to have the same value of the last values in the loop.

How can i get this fixed. Sample code is here;

  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

The values passed into the showDirection is that of the last item in array being iterated over. What could i be overlooking?

acacia
  • 1,375
  • 1
  • 14
  • 40

0 Answers0