Pregunta

How do pass color parameter to google maps api using google-maps-for-rails? I would like to set color of each marker inside controller based on a value, so some would be red, some yellow and some green. I believe it is the icon property, in Symbol, looking at:

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

Also, I would like to have a number from 1-99 inside the marker, is it possible? So far I have this.

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

I have been struggling with this for days, any help would be appreciated.

¿Fue útil?

Solución

You should simply leverage the google's chart api.

Example, the following is a marker with:

  • letter A
  • red color: FF0000
  • black text: 000000

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

So you should customize your needs:

@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

Otros consejos

I implemented the above solution to no avail, but now figured out why this wasn't working. Perhaps this was an update to the gem, but "url" is the right key for the URL of the picture, not "picture". Here was my code in my controller:

@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 

Hope this helps some people struggling with the same thing.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top