开发者

gmaps4rails display location based on latitude and longitude

I am using gmaps4rails with Rails 3.0.9. I would like to add a marker based on the latitude and longitude coordinates to the map, but I can't figure out how should I do it. I managed to use the map with an andress only. Thanks.

The code I'm using:

app/models/location.rb

class Location < ActiveRecord::Base
  acts_as_gmappable :process_geocoding => false

  def gmaps4rails_address
    "#{self.latitude}, #{self.longitude}"
  end
end

app/controllers/locations_controller.rb

def index
@json = Location.first.to_gmaps4rails

respond_to do |format|
  format.html
end

app/views/locations/index.html.erb

<%= gmaps4rails(@json) %>

The location model contains the following fields: latitude, longitude, address.

UPDATE: I found the solution after reading one more time project's wiki about markers. The solution is to use custom <%= gmaps %> method. I managed to use it like开发者_运维技巧 this:

<%= gmaps({
    "map_options" => { "type" => "ROADMAP", "center_longitude" => 28.9, "center_latitude" => 47.03, "zoom" => 12, "auto_adjust" => true},
    "markers" => { "data" => @markers }
    })
    %>

In the controller:

@markers = '[
             {"description": "", "title": "", "sidebar": "", "lng": "28.8701", "lat": "47.0345", "picture": "", "width": "", "height": ""},
             {"lng": "28.9", "lat": "47" }
            ]'

You can customize these values as you like.


I think you should try geocoder gem instead of gmaps4rails https://github.com/alexreisner/geocoder http://www.rubygeocoder.com/

I think it is much better than gmaps4rails

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜