KMLを全部出すと多すぎたので、緯度経度を指定してその周囲の写真だけ出す
成田帰りのバスで作った。
前ので4万件でwebricが固まったので、ちょっと考え直した。
file://localhost/opt/local/lib/ruby/gems/1.8/doc/activerecord-2.0.2/rdoc/classes/ActiveRecord/Base.html
を読むなどして ActiveRecord::Base.find_by_sql を使って、単純に四角く検索してみた
(例)
http://localhost:3000/photos.xml?lat=35&lon=135
http://localhost:3000/photos.kml?lat=35&lon=135
http://localhost:3000/photos?lat=35&lon=135&dist=1 などで検索して緯度経度のまわりの写真を出す(50件)
http://localhost:3000/photos
http://localhost:3000/photos.kml などではid順に50件取得
app/controllers/photos_controller.rb
def index dist = 3 dist = params[:dist].to_i if params[:dist] != nil lat = params[:lat].to_i if params[:lat] != nil lon = params[:lon].to_i if params[:lon] != nil if (lat != nil && lon != nil) @photos = Photo.find_by_sql ["SELECT * FROM photos WHERE ? < lat AND lat < ? AND ? < lon AND lon < ? LIMIT 50", lat - dist, lat + dist, lon - dist, lon + dist] else @photos = Photo.find(:all, :limit => 50) end respond_to do |format| format.html format.xml { render :xml => @photos } format.js { render :json => @photos } format.kml { render :kml => @photos } end end
jsonでも出してみたりした。
円形に検索するのなら、postgresのpostgisやMySQLの空間座標を使った方が良いらしいけど、とりあえずSQLiteなのでこれで行ってみる。