本文介绍了如何在Google-Maps-for-Rails中为标记添加半径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的模特地点由以下部分组成:

我使用的是Google-Maps-for-Rails创业板,我正在尝试显示圈子。 >


  • 经度

  • 纬度




  • 我没有问题显示标记:



    pre> #控制器:@markers = Place.all.to_gmaps4rails
    gmaps(markers=> {data=> @markers})

    不幸的是,它不包含:radius => ..
    我也知道可以显示如下的圆圈:

      gmaps(
    circles=> {data=>'[
    { 经度:-122.214897,latitude:37.772323,radius:1000000}
    ]',
    })

    有没有办法在标记哈希中包含radius属性?像:

      gmaps(circles=> {data=> @markers})

    可能是这样的:

      Place.all.each | place | place.merge!(:radius => 1000)... 

    可以,但我认为可能有更好的解决方案

    解决方案

    标记和圆圈背后的逻辑真的不同。



    在您的控制器中

      @markers = Place.all.to_gmaps4rails 
    @circles = Place。 all.map {| p | {:longitude => p.longitude,:latitude => p.latitude,:半径=>




    $ p
    $ b $ p








     <%= gmaps({markers=> {data=> @markers},
    circles=> { data=> @circles}
    })%>如果您不需要 infowindow

    或其他功能,您可以简单地执行:

    在您的控制器中

      @circles = Place.all.map {| p | {:longitude => p.longitude,:latitude => p.latitude,:半径=> 




    $ p
    $ b $ p








     <%= gmaps({markers=> {data=> @circles},
    circles=> { data=> @circles}
    })%>


    i am using the Google-Maps-for-Rails gem and I am now trying to display circles.

    My Model "Place" consists of:

    • longitude
    • latitude
    • radius
    • etc

    I have no problem to display markers:

    # Controller: @markers = Place.all.to_gmaps4rails
    gmaps("markers" => {"data" => @markers})
    

    Unfortunately it doens't include :radius => ..I also know that circles can be displayed like:

    gmaps(
    "circles" => { "data" => '[
                             {"longitude": -122.214897, "latitude": 37.772323, "radius":1000000}
                             ]',
    })
    

    Is there any way to include the radius attribute in the markers hash ? Like:

    gmaps("circles"     => { "data" => @markers })
    

    Probably something like:

    Place.all.each |place| place.merge!(:radius => 1000) ...
    

    would do, but I think there may be a nicer solution

    解决方案

    The logic behind markers and circles is really different.

    In your controller

    @markers = Place.all.to_gmaps4rails
    @circles = Place.all.map{|p| {:longitude => p.longitude, :latitude => p.latitude, :radius => p.radius}}.to_json
    

    In your view:

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

    Or if you don't need infowindow or other features, you could simply do:

    In your controller

    @circles = Place.all.map{|p| {:longitude => p.longitude, :latitude => p.latitude, :radius => p.radius}}.to_json
    

    In your view:

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

    这篇关于如何在Google-Maps-for-Rails中为标记添加半径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 22:26