4

如何使用 google-maps-for-rails 将颜色参数传递给 google maps api?我想根据一个值设置控制器内每个标记的颜色,所以有些是红色,有些是黄色,有些是绿色。我相信这是符号中的图标属性,正在查看:

https://developers.google.com/maps/documentation/javascript/reference#MarkerOptions

另外,我想在标记内有一个 1-99 的数字,可以吗?到目前为止,我有这个。

@json = Device.all.to_gmaps4rails do |device, marker|
end

我已经为此苦苦挣扎了好几天,任何帮助将不胜感激。

4

2 回答 2

15

您应该简单地利用谷歌的图表 api。

例如,以下是一个标记:

  • 字母 A
  • 红色:FF0000
  • 黑色文字:000000

http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=A|FF0000|000000

所以你应该定制你的需求:

@json = Device.all.to_gmaps4rails do |device, marker|
   marker.picture({
     :picture => "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=A|FF0000|000000", # up to you to pass the proper parameters in the url, I guess with a method from device
     :width   => 32,
     :height  => 32
   })
end
于 2012-10-02T15:05:01.587 回答
4

我实施了上述解决方案无济于事,但现在弄清楚了为什么这不起作用。也许这是对 gem 的更新,但“url”是图片 URL 的正确键,而不是“图片”。这是我的控制器中的代码:

@devices_hash= Gmaps4rails.build_markers(@devices) do |device, marker|
  ...
  marker.picture({
     :url => "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=A|007FFF|000000",
     :width   => 32,
     :height  => 32
  })
end 

希望这可以帮助一些在同一件事上苦苦挣扎的人。

于 2014-09-12T16:54:06.870 回答