0
在我的Rails應用程序中,我有谷歌地圖,我想基於座標(緯度,經度)顯示引腳。我有user.rb文件中的虛擬數據作爲數組,我試圖通過它映射,並將座標傳遞給谷歌地圖,但我缺少一些基本的東西,因爲所有的工作,如果我手動提供。那麼我該如何迭代,以便在地圖上顯示座標?遍歷數組數組並傳遞座標到谷歌地圖
#user.rb
class User < ApplicationRecord
COORDINATES = [[51.50853, -0.076132], [51.510357, -0.116773]]
def self.latitude
COORDINATES.map do |x|
x[0] # How to pass coordinates correctly?
end
end
def self.longitude
COORDINATES.map do |x|
x[-1] # How to pass coordinates correctly?
end
end
end
這是用戶控制指標的行動:
def index
@latitude = User.latitude
@longitude = User.longitude
end
這就是index.html.erb。我在這裏提供@latitude和@longitude。
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers([
{
"lat": <%= @latitude %>,
"lng": <%= @longitude %>
}
]);
});
請澄清愚蠢的人喜歡我,所以你要'標記= handler.addMarkers([{ 「LAT」:51.50853, 「LNG」:-0.076132}, {「lat」:51.510357,「lng」:-0.116773}])',對嗎? – Fallenhero
是的,當您寫入 – user5922898
時您會嘗試我的解決方案嗎? – Fallenhero