Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@solars
Created November 23, 2011 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solars/277894809fe04cbc29c7 to your computer and use it in GitHub Desktop.
Save solars/277894809fe04cbc29c7 to your computer and use it in GitHub Desktop.
## template
gmaps("markers" => {"data" => @json}, "map_options" => {:type => 'HYBRID', "processing" => "json", "auto_adjust" => true, "zoom" => 10})
## javascript
function drawItems(theBounds) {
var url = '/within.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) {
Gmaps.map.replaceMarkers(newItemData);
});
}
Gmaps.map.callback = function() {
google.maps.event.addListener(Gmaps.map.map, "dragend", function() {
var bounds = Gmaps.map.map.getBounds();
drawItems(bounds);
});
};
## controller
def within
# 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 = Spot.within(bounds)
else
@venues_within_bounds = Spot.all
end
respond_to do |format|
format.html # index.html.erb
format.json {
@data = @venues_within_bounds #collect { |v|
render :json => @data
}
end
end
## within template:
@data.to_gmaps4rails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment