我想获取一个数组并将数据加载到 json 对象中,但我不知道该怎么做。
行程控制器
def index
@trips = Trip.all
@markers = Array.new
@trips.each do |trip|
for marker in trip.markers
@markers.push(marker)
end
end
@gmaps_options =
{
"markers" => { "data" => '[{ "lng": "-99.9018131", "lat": "31.9685988"},
{ "lng": "-102.552784", "lat": "23.634501"},
{ "lng": "-122.3667", "lat": "40.5833"},
{ "lng": "-121.8356", "lat": "39.7400"}
]', },
"polylines" => { "data" => ' [ [
{"lng": -99.9018131, "lat": 31.9685988},
{"lng": -102.552784, "lat": 23.634501},
], [
{ "lng": "-122.3667", "lat": "40.5833"},
{ "lng": "-121.8356", "lat": "39.7400"}
] ]' }
}
respond_to do |format|
format.html # index.html.erb
format.json { render json: @trips }
end
end
经度和纬度现在是硬编码的,但我想将@markers 加载到经度纬度中,
"lng": @markers[0].longitude, "lat": @markers[0].latitude
这使得地图不渲染,只是一个灰色框。
在视图中,使用 gmaps(@gmaps_options) 调用地图
任何帮助将不胜感激。