简体   繁体   English

使用Geokit-Rails按起点和终点位置搜索信息

[英]Searching Post by Origin & Destination locations using Geokit-Rails

Ok what i have is a Trucking Load board where Truckers come to post there available Trucks. 好的,我有一个卡车载货板,卡车司机可以在上面张贴可用的卡车。 I have the Trucks posting. 我有卡车张贴。 But am having issues setting up the search functions and the way i need to associate different tables. 但是在设置搜索功能以及我需要关联不同表的方式时遇到了问题。

Rails 3
postgres

gem 'geokit-rails'

The way i have it now is I have a locations table setup like: 我现在拥有的方式是我有一个位置表设置,例如:

 class Location < ActiveRecord::Base

  attr_accessible :cs, :lat, :lon, :city, :state

  acts_as_mappable :default_units => :miles,
                   :default_formula => :sphere,
                   :distance_field_name => :distance,
                   :lat_column_name => :lat,
                   :lng_column_name => :lon

When someone post a truck it has and Origin and a Destination. 当有人张贴卡车时,它具有“起点”和“目的地”。 so it has 2 locations and have set it up like this: 因此它有2个位置,并按如下所示进行设置:

class Truck < ActiveRecord::Base
  attr_accessible :available, :company_id, :dest_id, :origin_id, :equipment, :origin, :dest
  belongs_to :origin, class_name: "Location", foreign_key: :origin_id
  belongs_to :dest, class_name: "Location", foreign_key: :dest_id
  belongs_to :company

With the way i have it set up i can get the location information from: 通过我设置的方式,我可以从以下位置获取位置信息:

Truck.find(1).origin  || Truck.find(1).dest

It will return the Location record associated with it 它将返回与其关联的位置记录

Now my issue is that i want to be able to write a search function to find any Trucks within a "given" amount of miles from origin || 现在我的问题是,我希望能够编写一个搜索功能来查找距原点“给定”英里内的所有卡车。 dest || 目标|| origin & Destination 出发地和目的地

I know i can do Location.within(25, :origin => "Springfield, Mo") and it will search all the locations and return the ones that are within 25 miles of Springfield Mo 我知道我可以执行Location.within(25, :origin => "Springfield, Mo") ,它将搜索所有位置并返回Springfield Mo的25英里范围内的位置

But how would i use this on Trucks where there is 2 locations (origin & dest) and they are associated with location id. 但是我将如何在有2个位置(起源和目的地)并且它们与位置ID相关联的卡车上使用它。

I currently have some other search Params already coded in and working just not sure how i could incorporate this into it: 我目前有一些其他的搜索参数已被编码,并且无法确定如何将其整合到其中:

def search(search)
    where = []
    where << PrepareSearch.states('dest', search.dest_states) unless search.dest_states.blank?
    where << PrepareSearch.states('origin', search.origin_states) unless search.origin_states.blank?
    where << PrepareSearch.location('origin', search.origin_id, search.radius) unless search.origin.blank?
    where << PrepareSearch.location('dest', search.dest_id, search.radius) unless search.dest.blank?
    where << PrepareSearch.equipment(search.equipment) unless search.equipment.blank?
    where << PrepareSearch.date('available', search.available, '<') unless search.available.blank?
    where = where.join(' AND ')
    Truck.where(where)
  end


module PrepareSearch
    def PrepareSearch.location(type, location, radius)
      loc = Location.find(location)

   *type will be origin/destination Location active record
   *location will be Location id
   *radius will be a given mileage

**This is where i need to figure out what to put here**

    end
end

Would it be better just to incorporate the equation: 合并方程会更好:

 def sphere_distance_sql(origin, units)
    lat = deg2rad(origin.lat)
    lng = deg2rad(origin.lng)
    multiplier = 3963.1899999999996 # for miles

    sphere_distance_sql(lat, lng, multiplier)
  end
  def sphere_distance_sql(lat, lng, multiplier)
    %|
      (ACOS(least(1,COS(#{lat})*COS(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*COS(RADIANS(#{qualified_lng_column_name}))+
      COS(#{lat})*SIN(#{lng})*COS(RADIANS(#{qualified_lat_column_name}))*SIN(RADIANS(#{qualified_lng_column_name}))+
      SIN(#{lat})*SIN(RADIANS(#{qualified_lat_column_name}))))*#{multiplier})
     |
  end

Ok Well I have figured out a solution to my problem... if there is a better one i would love to know. 好吧,我想出了解决我问题的方法...如果有更好的方法,我很想知道。

where << PrepareSearch.location_ids("origin", search.origin, search.radius) unless search.origin_id.blank?
where << PrepareSearch.location_ids("dest", search.dest, search.radius) unless search.dest_id.blank?


def PrepareSearch.location_ids(type, location, radius)
  if location.nil?
    return nil
  else
    loc = Location.find(location)
    location(type, loc, radius)
  end

end

def PrepareSearch.location(type, location, radius)
  locs = Location.within(radius, :origin => location.cs).pluck(:id)
  st = ""
  locs.each {|s| st += "'#{s}'," }
  st = st[0..-2]
  "#{type}_id IN (#{st})"
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM