【问题标题】:Dynamically load Google Maps Markers with gmaps4rails使用 gmaps4rails 动态加载谷歌地图标记
【发布时间】:2011-02-18 21:52:30
【问题描述】:

如何仅加载带有gmaps4rails 的地图边界内的标记?当然,在平移和/或缩放之后加载新的。

与此直接相关,如何获取地图的当前边界和缩放级别?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 google-maps gmaps4rails


    【解决方案1】:

    我是这样做的,我只在用户完成平移或缩放后替换标记,如果您需要不同的行为,请使用不同的事件侦听器:

    在你看来(index.html.erb):

    <%= gmaps({ "map_options" => { "zoom" => 15, 
                                   "auto_adjust" => false, 
                                   "detect_location" => true, 
                                   "center_on_user" => true }}, false, true) %>
    

    在视图底部添加:

    <script type="text/javascript" charset="utf-8">
    
    function gmaps4rails_callback() {
        google.maps.event.addListener(Gmaps4Rails.map, 'idle', function () {
            var bounds = Gmaps4Rails.map.getBounds();
            drawItems(bounds);
        });
    }
    
    </script>
    

    在 application.js 中(使用 jQuery):

    function drawItems(theBounds) {
        var url = '/venues.json/?sw_y=' + theBounds.getSouthWest().lng() + 
                               '&sw_x=' + theBounds.getSouthWest().lat() + 
                               '&ne_y=' + theBounds.getNorthEast().lng() +
                               '&ne_x=' + theBounds.getNorthEast().lat();
        $.get(url, function(newItemData) {
            Gmaps4Rails.replace_markers(newItemData);
        });
    }
    

    venues_controller#index:

    def index
        # Only pull venues within the visible bounds of the map
        if (params[:sw_y] && params[:sw_x] && params[:ne_y] && params[:ne_x])
            bounds = [ [params[:sw_x].to_f, params[:sw_y].to_f], 
                     [params[:ne_x].to_f, params[:ne_y].to_f] ]
            @venues_within_bounds = Venue.within_bounds(bounds)
        else
            @venues_within_bounds = Venue.all
        end   
    
        respond_to do |format|
            format.html # index.html.erb
            format.json { 
                @data = @venues_within_bounds.collect {|v| {
                         :longitude => v.longitude, 
                         :latitude => v.latitude, 
                         :picture => v.marker_picture, 
                         :title => v.marker_title 
                }
                render :json => @data
            }
        end
    end
    

    Venue.rb 模型(使用 mongodb 和 mongoid):

    def self.within_bounds(bounds)
        self.where(:location.within => {"$box" => bounds })
    end
    

    【讨论】:

    • 这是一个非常好的例子。只是一个简短的说明我相信当前版本的 gmaps4rails 的名称空间是 Gmaps.map.map 所以... google.maps.event.addListener(Gmaps.map.map, 'idle', function() { //do });
    【解决方案2】:

    哇,你真的为 gem 提供了很多反馈:)

    这是我的使用方法:

    • 为了只加载有用的标记,我使用geokit-rails3 和以下范围过滤它们:Location.in_bounds([@south_west_point, @north_east_point], :origin =&gt; @somewhere)

    • 在缩放或跨度时,我只依靠聚类来加快进程

    • 要配置、地图中心和原始缩放,请参阅here

    • 你应该自己编写获取当前边界的方法,考虑拉 :)

    【讨论】:

      猜你喜欢
      • 2011-03-31
      • 2015-01-10
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 2013-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多