我以为我已将此方法的所有Google地图API调用移至客户端。我错过了什么,但我无法弄清楚是什么。
我正在调用near
方法,该方法是geocoder gem的一部分。
Location.facilities.near(@center, 50, order: :distance).select(&:item_is_searchable)
以下是Locations模型中的地理编码器代码:
geocoded_by :full_address
after_validation :geocode
def full_address
string = ""
string << "#{address}\n" if address
string << "#{extended_address}\n" if extended_address
string << "#{city}, " if city
string << "#{state} " if state
string << zip_code if zip_code
string
end
这可能是专家地理编码器帮助我的足够信息。如果没有,这里有更多信息。还有很多信息。
我一直在我的rails应用程序中使用Geocoder gem取得了巨大成功,但现在我正在考虑对Google Maps API速率限制进行评分。
大约一个月前,我遇到了速率限制问题,因此我将大部分请求移至客户端。我想我错过了什么。
我认为最相关的步骤是5,因为那是我调用地理编码器gem来做我不理解的东西的地方,所以我会把它归零(并在上下文中捕获一些步骤4和6)
#searches_controller.rb
def by_location
@search = Search.find_nearby_instructors_and_facilities params[:coordinates]
render :json => @search
end
#search.rb
class Search
include ActiveModel::Serialization
include ActiveModel::SerializerSupport
attr_accessor :facilities, :instructors, :center
def self.find_nearby_instructors_and_facilities(center)
search = Search.new(center)
search.find_nearest if center
search
end
def initialize(center)
@center = center
@instructors = Set.new
@facilities = []
end
def find_nearest
@locations = Location.facilities.near(@center, 50, order: :distance).select(&:item_is_searchable)
@facilities = @locations.map(&:addressable_item)
@facilities.each do |facility|
active_instructors = facility.instructors.select(&:active?)
@instructors.merge(active_instructors)
end
end
end
以下是near方法为坐标(30.267153, -97.74306079999997)
SELECT locations.*, 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((30.267153 - locations.latitude) * PI() / 180 / 2), 2) + COS(30.267153 * PI() / 180) * COS(locations.latitude * PI() / 180) * POWER(SIN((-97.7430608 - locations.longitude) * PI() / 180 / 2), 2))) AS distance, CAST(DEGREES(ATAN2( RADIANS(locations.longitude - -97.7430608), RADIANS(locations.latitude - 30.267153))) + 360 AS decimal) % 360 AS bearing FROM "locations" WHERE "locations"."addressable_item_type" = 'Facility' AND (locations.latitude BETWEEN 29.54349408444576 AND 30.99081191555424 AND locations.longitude BETWEEN -98.58093480507472 AND -96.90518679492527 AND 3958.755864232 * 2 * ASIN(SQRT(POWER(SIN((30.267153 - locations.latitude) * PI() / 180 / 2), 2) + COS(30.267153 * PI() / 180) * COS(locations.latitude * PI() / 180) * POWER(SIN((-97.7430608 - locations.longitude) * PI() / 180 / 2), 2))) <= 50) ORDER BY distance
如果有更多信息可以提供帮助,请与我们联系。