2014-11-05 149 views
0

我有一个数组,其中包含30多个位置,我想在我的地图上显示。但是,我还没有找到一个演示如何迭代数组以便标记出现在地图上的源代码,而是选择严格编码地图纬度/经度值。Gmaps4rails循环访问数组

例如,这是我目前的代码,但它会返回错误:

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() 

注:我不能简单地使用Ruby方法,因为该表还必须与.js文件中的数据表中的数据进行交互.coffee文件。

如何在gmaps4rails方法中循环数组?

回答

-1

由于handler.addMarkers需要一个数组,为什么不使用jQuery.map并预先构建一个标记数组?

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() 
+0

我吮吸coffeescript - 所以这可能不能直接从蝙蝠,但你应该得到的一般想法。 – max 2014-11-05 02:05:10

+0

如何动态添加多个标记? – Sauron 2014-11-05 02:11:57

+0

您是否阅读了https://github.com/apneadiving/Google-Maps-for-Rails上的自述文件? – max 2014-11-05 02:12:38