问题
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