【发布时间】:2015-05-15 09:55:10
【问题描述】:
我正在尝试实现gmaps4rails gem 和geocoder gem,效果很好。在index.html.erb 页面上,我列出了我的船只并显示地图,如果我有超过 1 个相同的地址,我不能显示超过 1 个标记。我想展示类似的东西;
这是我的locations_controller;
def index
if params[:search].present?
@locations = Location.near(params[:search], 5) #Kac mile cevresinde aranıldıgının bilgisi bu km ile değişebilir
else
@locations = Location.all
end
@hash = Gmaps4rails.build_markers(@locations) do |location, marker|
marker.lat location.latitude
marker.lng location.longitude
marker.infowindow render_to_string(:partial => "/locations/my_template", :locals => { :object => location})
end
end
这是我的index.html.erb;
<script src="//maps.google.com/maps/api/js?v=3.13&sensor=false&libraries=geometry" type="text/javascript"></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js' type='text/javascript'></script>
<h3>Nearby locations</h3>
<ul>
<% @locations.each do |location| %>
<li><%=location.address %></li> (<%= location.distance.round(2) %> miles)
<% end %>
</ul>
<div style='width: 800px;'>
<div id="map" style='width: 800px; height: 400px;'></div>
</div>
<script type="text/javascript">
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
</script>
即使我有多个相同纬度和经度的地址,它也只显示 1 个标记。我该如何克服它?
编辑 1:
这是它的样子;
然后缩小;
这就是我按照你说的添加的;
<script type="text/javascript">
handler = Gmaps.build('Google', { markers: { maxRandomDistance: 5 } });
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
</script>
【问题讨论】:
标签: javascript ruby-on-rails google-maps google-maps-api-3