4

我正在使用带有 Rails 3.0.9 的 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

应用程序/视图/位置/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" }
            ]'

您可以根据需要自定义这些值。

4

1 回答 1

0

我认为您应该尝试使用 geocoder gem 而不是 gmaps4rails https://github.com/alexreisner/geocoder http://www.rubygeocoder.com/

我认为它比 gmaps4rails 好得多

于 2012-08-26T21:27:06.227 回答