I have a little problem that seems rather easy to solve but I can't seem to find a solution. I'm using the Geocoder gem for Rails and from this method of:
我有一个小问题似乎很容易解决,但我似乎无法找到解决方案。我正在使用Rails的Geocoder gem和以下方法:
Geocoder.coordinates(params[:search])
I'm getting an array that contains floats like this: [43.653226, -79.3831843]
我得到一个包含这样的浮点数的数组:[43.653226,-79.3831843]
My question is, is there a way to remove the brackets from the array or convert the value to a float so the output is 43.653226, -79.3831843
?
我的问题是,有没有办法从数组中删除括号或将值转换为浮点数,所以输出是43.653226,-79.3831843?
My closest solution so far has been to do this:
到目前为止,我最接近的解决方案是:
a = Geocoder.coordinates(params[:search])
and then to obtain each float individually by doing this in the method call:
然后通过在方法调用中执行此操作来单独获取每个浮点数:
a[0] --> 43.653226
a[-1] --> -79.3831843
This doesn't seem to work as I get an error with the particular method that I am trying to use.
这似乎不起作用,因为我尝试使用的特定方法出错。
I'm using the 'sunspot-solr' and 'sunspot-rails' gem for this and what I am trying to do is allow a user to enter a city to find users and then display users near that city within a 100, 200 km radius
我正在使用'sunspot-solr'和'sunspot-rails'宝石,我想做的是让用户进入一个城市寻找用户,然后在100,200 km范围内显示该城市附近的用户半径
This is what I have in my user.rb
file:
这是我在user.rb文件中的内容:
class User < ActiveRecord::Base
searchable do
text :city
latlon(:location) { Sunspot::Util::Coordinates.new(latitude, longitude) }
end
end
This is what's in my users_controller.rb
这就是我的users_controller.rb中的内容
def index
if params[:search]
@search = User.search do
fulltext params[:search]
with(:location).near(*Geocoder.coordinates(params[:search]),:precision => 6)
end
@users = @search.results
end
end
This is the line that seems to be causing the problem: with(:location).near(*Geocoder.coordinates(params[:search]),:precision => 6)
这是似乎导致问题的一行:with(:location).near(* Geocoder.coordinates(params [:search]),: precision => 6)
And finally in my view, this is the search form:
最后在我看来,这是搜索表单:
<%= form_tag users_path, class: "form-signin", role: "form", method: :get do %>
<%= text_field_tag :search, params[:search], class: "form-control", placeholder:"Type in a location" %>
<div><%= submit_tag "Search users", class: "btn btn-lg btn-primary btn-block", id: "home-search" %></div>
<% end %>
Now when I'm trying to add the geospatial feature I'm getting a 400 error that looks like this:
现在,当我尝试添加地理空间功能时,我收到400错误,如下所示:
RSolr::Error::Http - 400 Bad Request Error: {'responseHeader'=>{'status'=>400,'QTime'=>2},'error'=>{'msg'=>'com.spatial4j.core.exception.InvalidShapeException: incompatible dimension (2) and values (dpz83dffmxps). Only 0 values specified','code'=>400}}
If anyone can help that'd me great!
如果有人能帮助我,那就太棒了!
2 个解决方案
#1
0
The values are floats already, if you want to use the content of the array as 2 arguments to a method call then you can use the splat operator (*
) e.g
如果要将数组的内容用作方法调用的2个参数,那么值已经是浮点数,那么您可以使用splat运算符(*),例如
method_call(*[43.653226, -79.3831843]) is same as method_call(43.653226, -79.3831843)
or destructure the array and pass the new variables as argument e.g:
或者对数组进行解构并将新变量作为参数传递,例如:
lat, lng = [43.653226, -79.3831843]
method_call(lat,lng)
#2
0
You could use join: eg.
你可以使用join:例如。
a = Geocoder.coordinates(params[:search])
a.join(", ");
Would return the array - comma separated.
将返回数组 - 逗号分隔。
#1
0
The values are floats already, if you want to use the content of the array as 2 arguments to a method call then you can use the splat operator (*
) e.g
如果要将数组的内容用作方法调用的2个参数,那么值已经是浮点数,那么您可以使用splat运算符(*),例如
method_call(*[43.653226, -79.3831843]) is same as method_call(43.653226, -79.3831843)
or destructure the array and pass the new variables as argument e.g:
或者对数组进行解构并将新变量作为参数传递,例如:
lat, lng = [43.653226, -79.3831843]
method_call(lat,lng)
#2
0
You could use join: eg.
你可以使用join:例如。
a = Geocoder.coordinates(params[:search])
a.join(", ");
Would return the array - comma separated.
将返回数组 - 逗号分隔。