add geolocation search for public channels

This commit is contained in:
Lee Lawlor
2014-08-05 15:12:41 -04:00
parent 6d2d9f2b17
commit 2125d714e6
10 changed files with 64 additions and 1 deletions

View File

@ -6,6 +6,7 @@ class ChannelsController < ApplicationController
layout 'application', :except => [:social_show, :social_feed]
protect_from_forgery :except => [:realtime, :realtime_update, :post_data, :create, :destroy, :clear]
require 'csv'
require 'will_paginate/array'
# get list of all realtime channels
def realtime
@ -55,6 +56,10 @@ class ChannelsController < ApplicationController
elsif params[:tag].present?
@header = "#{t(:tag).capitalize}: #{params[:tag]}"
@channels = Channel.public_viewable.active.order('ranking desc, updated_at DESC').with_tag(params[:tag]).paginate :page => params[:page]
# get channels by location
elsif params[:latitude].present? && params[:longitude].present? && params[:distance].present?
@header = "#{t(:channels_near)}: [#{params[:latitude]}, #{params[:longitude]}]"
@channels = Channel.location_search(params).paginate :page => params[:page]
# normal channel list
else
@header = t(:featured_channels)

View File

@ -52,6 +52,11 @@
class Channel < ActiveRecord::Base
include KeyUtilities
# geolocation search: Channel.within(miles, :origin => [latitude, longitude]).to_a
# example: channels = Channel.within(4000, :origin => [4, 6]).to_a
# channels.sort_by{|s| s.distance_to([4, 6])}
acts_as_mappable :default_units => :kms, :default_formula => :sphere,
:distance_field_name => :distance, :lat_column_name => :latitude, :lng_column_name => :longitude
belongs_to :user
has_many :feeds
@ -85,6 +90,18 @@ class Channel < ActiveRecord::Base
cattr_reader :per_page
@@per_page = 15
# search for public channels within a certain distance from the origin
# requires latitude, longitude, and distance to be present as options keys
# distance is in kilometers
def self.location_search(options = {})
# set the origin
origin = [options[:latitude].to_f, options[:longitude].to_f]
# query the database
channels = Channel.public_viewable.within(options[:distance].to_f, :origin => origin)
# sort channels by distance
return channels.sort_by{|c| c.distance_to(origin)}
end
# how often the channel is updated
def update_rate
last_feeds = self.feeds.order('entry_id desc').limit(2)

View File

@ -16,6 +16,14 @@ Valid parameters:
<li><b>username</b> (string) Person's username that you want to search Channels for (optional)</li>
</ul>
<br>
You can also search for Channels within a certain distance of a location by including the following location parameters:
<ul>
<li><b>latitude</b> (decimal) - Latitude of location in degrees. (optional)</li>
<li><b>longitude</b> (decimal) - Longitude of location in degrees. (optional)</li>
<li><b>distance</b> (decimal) - Distance in kilometers from location. (optional)</li>
</ul>
<br>
Example GET:
@ -163,6 +171,7 @@ Valid parameters:
<li><b>api_key</b> (string) - Your Account API Key (this is different from a Channel API Key, and can be found in your Account settings). (required)</li>
</ul>
<br>
Example GET: