gpt4 book ai didi

javascript - Gmaps4rails Gem 显示同一地址的 2 个标记

转载 作者:行者123 更新时间:2023-12-02 16:10:37 26 4
gpt4 key购买 nike

我正在尝试实现gmaps4rails gemgeocoder gem,效果很好。在 index.html.erb 页面上,我列出了我的船只并显示 map ,如果我有超过 1 个相同的地址,我就无法显示超过 1 个标记。我想展示类似的东西;

enter image description here

这是我的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:

这是它的样子;

enter image description here

然后缩小;

enter image description here

这就是我按照你所说添加的;

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

最佳答案

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

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

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

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

关于javascript - Gmaps4rails Gem 显示同一地址的 2 个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30256485/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com