【问题标题】:Gmaps4rails Gem show 2 markers for same addressGmaps4rails Gem 显示相同地址的 2 个标记
【发布时间】:2015-05-15 09:55:10
【问题描述】:

我正在尝试实现gmaps4rails gemgeocoder 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&amp;sensor=false&amp;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


    【解决方案1】:

    这是任何地图的问题:你不能在同一个地方有两个东西。

    由于这是一个常见问题,但 gem 中有一个选项:

    handler = Gmaps.build('Google', { markers: { maxRandomDistance: integerInMeters } });
    

    这将稍微随机化您在预期中心的maxRandomDistance 距离内给出的坐标

    【讨论】:

    • 谢谢,我在 gmaps4rails wiki 中读到了 randomize。我认为这是同一件事吗?但是当我通过参数并缩小地图时。现在它不会像我发布的图片那样总结/组合标记。我应该添加其他参数吗?应该是自动的吧?
    • 我不明白你做了什么,但如果你按照我写的,它应该是自动的
    • 我想我应该编辑缩小属性。当我完全缩小地图时,它会显示它们的总和。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    相关资源
    最近更新 更多