Gmaps4rails循环通过数组 [英] Gmaps4rails looping through array

查看:287
本文介绍了Gmaps4rails循环通过数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,有30多个地点,我想在我的地图上显示。但是,我还没有找到一个源,演示如何迭代数组,以便标记出现在地图上,而是选择严格硬编码地图lat / long值。

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

注意:我不能简单地使用ruby方法,因为表必须也与.js.coffee文件中的DataTable数据交互。

Note: I cannot simply use the ruby methods because the table must also interact with DataTable data within the .js.coffee file.

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

How can I loop through the array within the gmaps4rails method?

推荐答案

c $ c> handler.addMarkers 使用数组为什么不只是使用jQuery.map并建立一个标记数组?

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

这篇关于Gmaps4rails循环通过数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆