5

gmaps4railsでマップ境界内にあるマーカーのみをロードするにはどうすればよいですか? もちろん、パンやズームの後に新しいものをロードします。

それに直接関連して、マップの現在の境界とズームレベルを取得するにはどうすればよいですか?

4

2 に答える 2

10

これが私が行った方法です。ユーザーがパンまたはズームを終了した後にのみマーカーを置き換えます。別の動作が必要な場合は、別のイベントリスナーを使用します。

ビュー (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);
    });
}

会場_コントローラー#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
于 2011-05-24T21:49:10.690 に答える
2

うわー、あなたは本当に宝石に多くのフィードバックを提供します:)

これが私がそれを使う方法です:

  • 有用なマーカーのみをロードするには、geokit-rails3と次のスコープを使用してそれらをフィルタリングします。Location.in_bounds([@south_west_point, @north_east_point], :origin => @somewhere)

  • ズームまたはスパンする場合、プロセスを高速化するクラスタリングのみに依存します

  • 設定、マップセンター、元のズームについては、こちらをご覧ください

  • 現在の境界を取得するメソッドを自分でコーディングする必要があります。プルすることを検討してください:)

于 2011-02-18T22:29:48.160 に答える