javascriptruby-on-railsgmaps4rails

Gmaps4Rails Drawing Circles relative to one dynamic point


This may be a javascript question but since it also deals with Rails variables and gmpas4rails gem I would like to ask you anyway...

I have this in my view

<%= gmaps(
    { 
     "markers"     => { "data" => @json },

     "circles"     => { "data" => '[
                         {"lng": 122.214897, "lat": 37.772323, "radius": 25000, "strokeColor": "#FF0000"}
                         ]',
                      },
    }
        ) %>

And the part where I have circles I want to have this

"circles"     => { "data" => '[
                         {"lng": '@shop.longitude', "lat": '@shop.latitude', "radius": 25000, "strokeColor": "#FF0000"}

So for every shop it would center the circles and draw the zone...


Solution

  • You should not escape the latitude and longitude:

    "circles" => { 
      "data" => "[{'lng': #{@shop.longitude}, 'lat': #{@shop.latitude}, 'radius': 25000, 'strokeColor': '#FF0000'}]"
    }
    

    Alternate solution:

    "circles" => { 
      "data" => [{'lng' => @shop.longitude, 'lat' => @shop.latitude, 'radius' => 25000, 'strokeColor' => '#FF0000'}].to_json
    }