Gmaps4rails looping through array

╄→гoц情女王★ 提交于 2019-12-25 06:54:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!