I am using PostGIS (1.5.1) with Rails (3.0.0rc) and Google Map (v3), and I am looking for a way to query the database to retrieve all the points in the polygon (in particular, the border of the map view window). I am currently sending border coordinates to the server as such:
google.maps.event.addListener(map, 'bounds_changed', function() {
var bounds = map.getBounds();
var ne = bounds.getNorthEast();
var sw = bounds.getSouthWest();
var yMaxLat = ne.lat();
var xMaxLng = ne.lng();
var yMinLat = sw.lat();
var xMinLng = sw.lng();
updateMap(yMaxLat, xMaxLng, yMinLat, xMinLng);
});
function updateMap(yMaxLat, xMaxLng, yMinLat, xMinLng) {
jQuery.getJSON("/places", { "yMaxLat": yMaxLat, "xMaxLng": xMaxLng, "yMinLat": yMinLat, "xMinLng": xMinLng }, function(json) {
if (json.length > 0) {
for (i=0; i<json.length; i++) {
var place = json[i];
var category = json[i].tag;
addLocation(place,category);
}
}
});
}
Then in my controller:
format.js do
xMinLng = params[:xMinLng]
yMinLat = params[:yMinLat]
xMaxLng = params[:xMaxLng]
yMaxLat = params[:yMaxLat]
@map_places = Place.find(:all, :conditions => ["geom && ?", Polygon.from_coordinates([[[xMinLng, yMinLat], [xMinLng, yMaxLat], [xMaxLng, yMaxLat], [xMaxLng, yMinLat], [xMinLng, yMinLat]]], 4326)])
render :json => @map_places
end
, , , - , , (, ). . - PostGIS, , , GeoRuby ( GeoDjango). :
Started GET "/places?yMaxLat=63.93767251746141&xMaxLng=43.033828125&yMinLat=36.88016688406946&xMinLng=-39.407578125" for 127.0.0.1 at Tue Aug 24 12:11:41 +0100 2010
Processing by PlacesController
Parameters: {"xMinLng"=>"-39.407578125", "yMaxLat"=>"63.93767251746141", "xMaxLng"=>"43.033828125", "yMinLat"=>"36.88016688406946"}
Place Load (0.4ms) SELECT "places".* FROM "places"
Place Load (0.8ms) SELECT "places".* FROM "places" WHERE (geom && '0103000020E6100000010000000500000052B81E852BB443C0DF0CF74EA970424052B81E852BB443C0686D2EA705F84F40AE47E17A54844540686D2EA705F84F40AE47E17A54844540DF0CF74EA970424052B81E852BB443C0DF0CF74EA9704240')
Completed 200 OK in 136ms (Views: 3.4ms | ActiveRecord: 1.2ms)
? GeoRuby space_adapter gems , , . , ? !
EDIT: :
class CreatePlaces < ActiveRecord::Migration
def self.up
create_table :places do |t|
t.string :city_name
t.integer :country_id, :null => false
t.point :geom, :null => false, :with_z => true, :srid => 4326
t.string :name, :null => false
t.string :tag, :null => false
t.timestamps
end
add_index :places, :geom, :spatial => true
end
def self.down
drop_table :places
end
remove_index :places, :geom
end