如何为位置添加半径和颜色

时间:2017-03-16 06:31:05

标签: ruby-on-rails gmaps4rails

我是gmap4rails gem的新手 我在维基https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Circles

中看到了这一点

但是当我用我的代码尝试它时 我放这个

  def index
    @ads = Ad.all
    @hash = Gmaps4rails.build_markers(@ads) do |ads, marker|
  marker.lat ads.latitude
  marker.lng ads.longitude
  marker.infowindow ads.name
  end
@circles_json = '[
 {"lng": -122.214897, "lat": 37.772323, "radius": 1000000},
 //you can add whatever customization within the hash
 {"lng": 122.214897, "lat": 37.772323, "radius": 1000000, "strokeColor": "#FF0000"}
]'
  end

<script type="text/javascript">
  handler = Gmaps.build('Google');
  handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
   markers = handler.addMarkers(<%=raw @hash.to_json %>);
   circle = handler.addMarkers(<%= gmaps({"circles"=> { "data" => @circles_json }})%>);
   handler.bounds.extendWith(markers);
   handler.fitMapToBounds();

 });
</script>

和文件application.js

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require underscore
//= require gmaps/google
//= require_tree .

并得到了这个问题 enter image description here

2 个答案:

答案 0 :(得分:0)

您是否在dom中插入了Google脚本?

您是否在application.js中添加了以下行?

//= require underscore
//= require gmaps/google

答案 1 :(得分:0)

尝试

<script type="text/javascript">
    handler = Gmaps.build('Google');
    handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
        markers = handler.addMarkers(<%=raw @hash.to_json %>);

        var circles = handler.addCircles(
            [
                {"lng": 122.214897, "lat": 37.772323, "radius": 1000000, "strokeColor": "#FF0000"}
            ]
        );

        handler.bounds.extendWith(markers);
        handler.bounds.extendWith(circles);
        handler.fitMapToBounds();

    });

</script>