简体   繁体   English

Rails地理编码器附近

[英]Rails geocoder near

I want to return a list of job postings that are near a location (based on [latitude, longitude]). 我想返回一个位置附近的职位发布列表(基于[纬度,经度])。 I am using geocoder and it seems to be working. 我正在使用地理编码器 ,它似乎正在工作。 I can import a CSV file with a list of addresses and geocoder populates the latitude and the longitude columns. 我可以导入带有地址列表的CSV文件,然后geocoder填充纬度和经度列。

But I can't seem to get the near or nearby functions to work. 但是我似乎无法使nearnearby功能正常工作。 It returns a undefined method error. 它返回一个undefined method错误。 I've tried following this: Rails Geocoder - Still learning which explains that nearby is a model function. 我尝试了以下操作: Rails Geocoder-仍在学习 ,它解释了附近是模型函数。 But the example there did not work for me either. 但是那里的例子对我也不起作用。

This is what I have in my show.html.erb: 这就是我的show.html.erb中的内容:

<%= @jobposting.latitude %>, <%= @jobposting.longitude %>

40.7494011, -73.9389437 40.7494011,-73.9389437

<% @location = Jobposting.near([@jobposting.latitude, @jobposting.longitude], 150) %>
<%= @location.inspect %>

It does not error but returns a empty array: #<ActiveRecord::Relation []> when there are points within 10 miles of the latitude and longitude. 它不会出错,但会返回一个空数组: #<ActiveRecord::Relation []>当在经纬度10英里内有点时。

I'm unsure if I am calling it incorrectly, based on the documentation it looks like near is used with the model but not limited to calling it in the model. 我不确定我是否正确调用它,根据它看起来像对文档的near用来与模型,但不限于调用它的模型。

As a partial answer, I believe the method is nearbys , not nearby . 作为一个局部的答案,我相信该方法nearbys ,而不是nearby That might explain the undefined method error you mentioned. 这可能可以解释您提到的未定义方法错误。 I don't know why you aren't getting results for the near function. 我不知道为什么您没有得到near函数的结果。 The code you posted for the near method looks okay to me. 您为near方法发布的代码对我来说还不错。

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

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