2011-08-09 58 views
4

我在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" } 
      ]' 

,你喜歡,你可以自定義這些值。

+0

你能後的代碼,您正在使用現在? –

+4

不錯,你閱讀文檔,+1 :) – apneadiving

回答

0

我想你應該嘗試地理編碼器的寶石,而不是gmaps4rails https://github.com/alexreisner/geocoder http://www.rubygeocoder.com/

我覺得它比gmaps4rails好得多

+0

謝謝,它幫助 – SpeedyWizard

+2

@AlexanderGiraldo可能是我錯了,但我不認爲你可以在地圖上使用'Geocoder' – mrudult