我正在为我的项目使用ruby geocoder gem,随着项目的不断发展,我开始考虑连接到Google API密钥.将此添加到项目后:
Geocoder.configure do |config|
# geocoding service (see below for supported options):
config.lookup = :google
# to use an API key:
config.api_key = 'my_key'
# geocoding service request timeout, in seconds (default 3):
config.timeout = 5
end
Run Code Online (Sandbox Code Playgroud)
我收到Google Geocoding API错误:请求被拒绝.当我启动应用程序时.从阅读开始,如果他们选择继续使用宝石,似乎其他人转向雅虎.我可以将gem配置为使用google api密钥吗?主要是,我想留意每日查询的数量,以避免超过限制.
https://github.com/alexreisner/geocoder
我找不到从ip地址显示访客城市的文档?我知道如何使用HTML5,但我想使用地理编码器的价值.谢谢
class User < AR
has_many :locations
end
class Location < AR
belongs_to :user
geocoded_by :address
end
Run Code Online (Sandbox Code Playgroud)
如何编写执行此操作的查询?
@users = User._a_query_that_returns_users_in_'Paris'_
Run Code Online (Sandbox Code Playgroud) 我有这个优秀的宝石https://github.com/alexreisner/geocoder在我的应用程序中正常工作,但我需要略微不同地订购活动记录查询的结果.
例如,使用rails控制台
$> Person.near('London',10)
Run Code Online (Sandbox Code Playgroud)
生成一个长SQL查询(不相关)后跟
ORDER BY distance ASC
Run Code Online (Sandbox Code Playgroud)
这给出了一个很好的有序人员列表,从最近到最远(最远10英里).现在,如果我这样做
$> Person.near('London',10).order('price_per_hour')
Run Code Online (Sandbox Code Playgroud)
查询附加
ORDER BY distance ASC, price_per_hour
Run Code Online (Sandbox Code Playgroud)
我仍然想要伦敦附近的所有人,但我不希望它按距离排序.似乎无论如何都没有把它拿出来.
我无法在活动记录查询之外进行排序,因为我正在使用分页,因此需要在主查询中进行排序,然后对其进行分页.
我正在使用Ryan Bates在第273集中介绍的Ruby的地理编码器宝石http://railscasts.com/episodes/273-geocoder?view=asciicast.在那一集中,他使用以下内容搜索半径50公里范围内的对象(尽管他使用的是位置模型,我使用的是用户模型).
@users = User.near(params[:search], 50, :order => :distance)
Run Code Online (Sandbox Code Playgroud)
当我在带有postgres的Rails 4应用程序上运行此代码时,它说
PG::Error: ERROR: column users.distance does not exist
Run Code Online (Sandbox Code Playgroud)
在Ryan的演示应用程序(代码https://github.com/railscasts/273-geocoder/tree/master/siteseer-after)中,他的位置模型上没有距离列(因为我没有它我的用户模型)所以我认为这distance是由Geocoder gem提供的东西.但是,查看地理编码器的文档http://rdoc.info/github/alexreisner/geocoder/master/frames,没有distance方法,所有其他类似距离的方法都会产生相同的错误.
有谁知道我如何通过距离在该查询中订购结果?
更新在地理编码器主页http://www.rubygeocoder.com/上给出了这个使用示例distance,但是当我尝试它时它说用户不存在.
nearbys = Place.near("Omaha, NE", 50,
:order => "distance")
bearing = nearbys.first.bearing # => 46.12
Geocoder::Calculations.compass_point(
bearing) # => "NE"
Run Code Online (Sandbox Code Playgroud)
更新
这是生成的sql
ActionView::Template::Error (PG::Error: ERROR: column users.distance does not exist
LINE 1: ...ngitude) * PI() / 180 / 2), 2))) <= 50) ORDER BY "users".di... …Run Code Online (Sandbox Code Playgroud) 我正在使用地理编码器gem,但我不知道在哪个文件中我必须粘贴此代码.你能告诉我吗 ?
我创建了一个提供搜索条件的表单:
= search_form_for @q do |f|
%h3
Search:
= f.label :category_id
%br
= f.collection_select :category_id_eq, Category.all, :id, :name, :include_blank => true
%br
= f.label "Sub-Category"
%br
= f.collection_select :subcategory_id_eq, Subcategory.all, :id, :name, :include_blank => true, :prompt => "select category!"
%br
= f.label "Contains"
%br
= f.text_field :title_or_details_cont
%br
= f.submit
Run Code Online (Sandbox Code Playgroud)
我希望能够根据Rails Geocoder gem的"Near"功能进行搜索.有没有人知道如何合并现有的范围,或者特别是如何在Meta Search或Ransack中使用"近"范围?
到目前为止,我的所有尝试都是徒劳的.
我正在尝试设置我的RSpec测试以使用存根而不是使用网络来进行地理编码.
我补充说:
before(:each) do
Geocoder.configure(:lookup => :test)
Geocoder::Lookup::Test.add_stub(
"Los Angeles, CA", [{
:latitude => 34.052363,
:longitude => -118.256551,
:address => 'Los Angeles, CA, USA',
:state => 'California',
:state_code => 'CA',
:country => 'United States',
:country_code => 'US'
}],
)
Run Code Online (Sandbox Code Playgroud)
结束
我正在使用FactoryGirl来创建测试数据,如下所示:
FactoryGirl.define do
factory :market do
city 'Los Angeles'
state 'CA'
radius 20.0
end
end
Run Code Online (Sandbox Code Playgroud)
正确地对纬度/经度进行地理编码并以纬度/经度存储.但是,当我尝试:
Market.near(params[:search])
Run Code Online (Sandbox Code Playgroud)
它返回nil ..但是,如果我只是使用lookup =>:google它就像我打算一样.有没有人以前有这个工作,特别是地理编码器的近距离方法?
在我的Ruby on Rails应用程序中,我正在使用Geocoder.它工作正常,但我的测试慢了十倍!我找到了一些解决方案,但在我看来他们不是很清楚?有没有办法在测试环境中禁用Geocoder?
Rails 3.1,Ruby 1.9.3
所以我有两个模型,都有两组坐标.
第一种模式,Load具有from_lat/lng,以及to_lat/lng.
第二个型号,卡车,current_lat/lng以及destination_lat/lng.
我要做的是找到所有结果,current_lat/lng或者destination_lat/lng来自Load模型的to_lat/lng附近的Truck模型.
我的车道模型是这样的:
class Load < ActiveRecord::Base
has_many :trucks
geocoded_by :custom_geocode
after_validation :custom_geocode
def custom_geocode
var = Geocoder.coordinates([from_city,from_state].compact.join(','))
var2 = Geocoder.coordinates([to_city,to_state].compact.join(','))
self.from_lat = var.first
self.from_lng = var.last
self.to_lat = var2.first
self.to_lng = var2.last
end
end
Run Code Online (Sandbox Code Playgroud)
在我看来,我有:
<% for truck in @trucks %>
<li><%= truck.destination_lat %></li>
<% end %>
Run Code Online (Sandbox Code Playgroud)
目前我在load_controller中使用它:
def show
@load = Load.find(params[:id])
@trucks = Truck.near([@load.from_lat, @load.from_lng], 100, :order => …Run Code Online (Sandbox Code Playgroud)