How to add a radius to a marker in Google-Maps-for-Rails?

落爺英雄遲暮 提交于 2019-12-08 09:09:33

问题


i am using the Google-Maps-for-Rails gem and I am now trying to display circles.

My Model "Place" consists of:

  • longitude
  • latitude
  • radius
  • etc

I have no problem to display markers:

# Controller: @markers = Place.all.to_gmaps4rails
gmaps("markers" => {"data" => @markers})

Unfortunately it doens't include :radius => .. I also know that circles can be displayed like:

gmaps(
"circles" => { "data" => '[
                         {"longitude": -122.214897, "latitude": 37.772323, "radius":1000000}
                         ]',
})

Is there any way to include the radius attribute in the markers hash ? Like:

gmaps("circles"     => { "data" => @markers })

Probably something like:

Place.all.each |place| place.merge!(:radius => 1000) ...

would do, but I think there may be a nicer solution


回答1:


The logic behind markers and circles is really different.

In your controller

@markers = Place.all.to_gmaps4rails
@circles = Place.all.map{|p| {:longitude => p.longitude, :latitude => p.latitude, :radius => p.radius}}.to_json

In your view:

<%= gmaps({ "markers" => {"data" => @markers},
           "circles" => {"data" => @circles}
          }) %>

Or if you don't need infowindow or other features, you could simply do:

In your controller

@circles = Place.all.map{|p| {:longitude => p.longitude, :latitude => p.latitude, :radius => p.radius}}.to_json

In your view:

<%= gmaps({ "markers" => {"data" => @circles},
           "circles" => {"data" => @circles}
          }) %>


来源:https://stackoverflow.com/questions/5719181/how-to-add-a-radius-to-a-marker-in-google-maps-for-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!