Aquí es cómo lo hice, sólo sustituyo los marcadores después de que el usuario termina de paneo o zoom, si se requiere un comportamiento diferente a continuación, utilizar un detector de eventos diferentes:
En su opinión (index.html.erb):
<%= gmaps({ "map_options" => { "zoom" => 15,
"auto_adjust" => false,
"detect_location" => true,
"center_on_user" => true }}, false, true) %>
En la parte inferior de la vista de añadir:
<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>
En application.js (usando 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 N.º:
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
modelo Venue.rb (usando mongodb y mongoid):
def self.within_bounds(bounds)
self.where(:location.within => {"$box" => bounds })
end
esa es una gran respuesta, +1 – apneadiving
sí, ¡gracias por eso! – jmk
Este es un gran ejemplo. Solo una nota rápida Creo que el espacio de nombre con la versión actual de gmaps4rails es Gmaps.map.map entonces ... google.maps.event.addListener (Gmaps.map.map, 'inactivo, función() { // hacer algo }); – Nick