问题
I have an array with 30+ locations I would like to display on my map. However, I have not found one source that demonstrates how to iterate through an array so that the markers appear on a map, but instead opt to strictly hard code the map lat/long values.
For example, this is the code I have at present but it returns errors:
allLocations = root.table.rows().data()
root.forMap = []
for aLocation in allLocations
root.forMap.push(aLocation[9] + ', ' + aLocation[10])
$('#multi_markers').map ->
handler = Gmaps.build("Google")
handler.buildMap
internal:
id: "multi_markers"
, ->
markers = handler.addMarkers(root.forMap)
handler.bounds.extendWith markers
handler.fitMapToBounds()
Note: I cannot simply use the ruby methods because the table must also interact with DataTable data within the .js.coffee file.
How can I loop through the array within the gmaps4rails method?
回答1:
Since handler.addMarkers
takes an array why not just use jQuery.map and build an array of markers beforehand?
all_locations = $.map root.table.rows().data(), (row)->
return {
lat: row[9],
lng: row[10]
}
$('#multi_markers').map ->
handler = Gmaps.build("Google")
handler.buildMap
internal:
id: "multi_markers"
, ->
markers = handler.addMarkers(allLocations)
handler.bounds.extendWith markers
handler.fitMapToBounds()
来源:https://stackoverflow.com/questions/26748320/gmaps4rails-looping-through-array