2014-09-29 55 views
0

在地面上使用gmaps4rails進行地圖功能:在控制器中結合變量

這個想法是在地圖上顯示房子的標記加上多個景點。

我有這段代碼;

控制器:

@house = House.friendly.find(params[:id]) 
@location = @house.location 
@sights_markers = Location.where("category = 'sights'") 

records = [@location, @sights_markers].compact 

@hash = Gmaps4rails.build_markers(records) do |location, marker| 
    marker.lat location.latitude 
    marker.lng location.longitude 

end 

觀點:

<div id="map" style='width: 100%; height: 500px; margin-top: 10px; margin-bottom: 20px;' ></div> 

<script type=text/javascript> 

    handler = Gmaps.build('Google'); 
    handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){ 
     markers = handler.addMarkers(<%=raw @hash.to_json %>); 
     handler.bounds.extendWith(markers); 
     handler.fitMapToBounds(); 


    }); 
</script> 

我得到發現 「緯度」

我在做什麼錯了一個錯誤message..no方法?

+0

什麼是你的位置表的方案?它有'緯度'字段嗎? – DiegoSalazar 2014-09-29 17:05:33

回答

1

重點是有一個數組。因此,更換:

records = [@location, @sights_markers].compact 

records = @sights_markers.to_a + [@location] 
+0

非常感謝你! – Remco 2014-09-29 21:17:56