-
-
Save solars/277894809fe04cbc29c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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