Setting marker color when using gogle-maps-for-rails in Rails

How to pass color parameter in google maps api using google-maps-for-rails? I would like to set the color of each marker inside the controller based on the value, so some of them will be red, yellow and green. I believe this is an 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 this possible? While I have it.

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

I struggled with this for several days, any help would be appreciated.

+4
source share
2 answers

You should just use google chart.

For example, the following marker:

  • 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 have to 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 
+15
source

I implemented the above solution to no avail, but now I understand why this does not work. It may have been an update for the gem, but "url" is the correct key for the image URL, not the "image". Here is my code in the 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 fight the same.

+4
source

Source: https://habr.com/ru/post/1437386/


All Articles