我在Rails 3.0.9中使用gmaps4rails。我想添加一個基於緯度和經度座標的標記到地圖,但我無法弄清楚該怎麼做。我設法只用一個女主人來使用地圖。謝謝。gmaps4rails根據經度和經度顯示位置
我正在使用的代碼:
應用程序/模型/ location.rb
class Location < ActiveRecord::Base
acts_as_gmappable :process_geocoding => false
def gmaps4rails_address
"#{self.latitude}, #{self.longitude}"
end
end
應用程序/控制器/ locations_controller.rb
def index
@json = Location.first.to_gmaps4rails
respond_to do |format|
format.html
end
應用程序/視圖/ locations/index.html.erb
<%= gmaps4rails(@json) %>
location
模型包含以下字段:latitude, longitude, address
。
更新:我在再次閱讀有關標記的項目wiki後找到了解決方案。解決方案是使用自定義<%= gmaps %>
方法。我設法用這樣的:
<%= gmaps({
"map_options" => { "type" => "ROADMAP", "center_longitude" => 28.9, "center_latitude" => 47.03, "zoom" => 12, "auto_adjust" => true},
"markers" => { "data" => @markers }
})
%>
在控制器:
@markers = '[
{"description": "", "title": "", "sidebar": "", "lng": "28.8701", "lat": "47.0345", "picture": "", "width": "", "height": ""},
{"lng": "28.9", "lat": "47" }
]'
,你喜歡,你可以自定義這些值。
你能後的代碼,您正在使用現在? –
不錯,你閱讀文檔,+1 :) – apneadiving