如何在一张静态地图上有多个标记?

How to have multiple markers on one static map?

我想在一张静态地图上设置多个标记。

我为此使用 geocoder gem (followed the railscast tutorial)。设法让它工作。

现在,我试图在索引页上循环显示一张地图上的所有标记,但它会分别显示在每张地图上。

这是索引页上的代码:

<% @locations.each do |location| %> 
<%= image_tag "http://maps.google.com/maps/api/staticmap?size=450x300&sensor=false&zoom=15&markers=#{location.latitude}%2C#{location.longitude}" %>
<% end %>

我如何遍历它,以便所有标记都显示在 one 地图上,而不是每个 longtitude/latitude?

的地图上

如有任何建议,我们将不胜感激,请让我知道我需要包含哪些进一步的信息。

它单独显示每个地图,因为您为每个标记打印图像。你想要的是,一个 URL 并在每个标记的末尾添加 &markers=#{location.latitude}%2C#{location.longitude}

因此您的代码应如下所示:

<% my_url = "http://maps.google.com/maps/api/staticmap?size=450x300&sensor=false&zoom=15" %>
<% @locations.each do |location| %> 
<% my_url += "&markers=#{location.latitude}%2C#{location.longitude}" %>    
<% end %>
<%= image_tag my_url %>

Matej 的回答是正确的,但是从助手那里调用它会更好。可能是这样

  def nice_gmap_static(location)
    center = "#{location.latitude},#{location.longitude}"
    "https://maps.googleapis.com/maps/api/staticmap?center=#{center}&size=300x300&zoom=17&markers=#{location.latitude}%2C#{location.longitude}"
  end