Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用Ruby Geocoder检索附近用户和返回距离_Sql_Ruby On Rails_Ruby_Rails Geocoder - Fatal编程技术网

Sql 使用Ruby Geocoder检索附近用户和返回距离

Sql 使用Ruby Geocoder检索附近用户和返回距离,sql,ruby-on-rails,ruby,rails-geocoder,Sql,Ruby On Rails,Ruby,Rails Geocoder,是否可以使用地理编码器检索(例如)某个半径内的附近用户,并使其返回距离计算结果而无需重复显示 例如,假设我们使用这个(在Rails应用程序中): 它将返回一个ActiveRecord::Relation与距离user@me10公里以内的所有用户的关系 如果我现在想显示一个包含所有返回用户及其到@me的距离的列表,我是否需要使用distance\u to逐个重新计算距离,或者是否有办法让Geocoder在添加的模型属性中返回计算出的距离(例如) 运行位置感知查询时,返回的对象有两个 添加到它们的属

是否可以使用地理编码器检索(例如)某个半径内的附近用户,并使其返回距离计算结果而无需重复显示

例如,假设我们使用这个(在Rails应用程序中):

它将返回一个
ActiveRecord::Relation
与距离user
@me
10公里以内的所有用户的关系

如果我现在想显示一个包含所有返回用户及其到
@me
的距离的列表,我是否需要使用
distance\u to
逐个重新计算距离,或者是否有办法让
Geocoder
在添加的模型属性中返回计算出的距离(例如)

运行位置感知查询时,返回的对象有两个 添加到它们的属性:

  • obj.distance—从搜索点到该对象的英里数
  • obj.方位-从搜索点到该对象的方向

我使用的是这个嵌入式Ruby版本的逻辑,它在逻辑上类似于上面@maxcal给出的答案。我之所以包含它,是因为下面的跟踪与此代码相关

<% for location in @location.nearbys(20) %>
  <li> <span class="badge"><%= location.distance.round(2) %> miles</span> from here you'll find <strong>
  <%= link_to location.name, location %> </strong> at <%= location.full_address %>.</li>
<% end %> 

谢谢你@Valerie,现在一切都有意义了!
User.near(@me, 10, units: :km).each do |user|
  puts "#{user.name} is #{user.distance} clicks to the #{user.bearing}"
end
<% for location in @location.nearbys(20) %>
  <li> <span class="badge"><%= location.distance.round(2) %> miles</span> from here you'll find <strong>
  <%= link_to location.name, location %> </strong> at <%= location.full_address %>.</li>
<% end %> 
  [1m[36m (0.0ms)[0m  [1mSELECT COUNT(*) FROM "reviews" WHERE "reviews"."user_id" = $1[0m  [["user_id", 8]]
  Rendered shared/_user_posted_badge.html.erb (15.6ms)
// Comment Geocoding starts here
  Rendered locations/_map_show.html.erb (0.0ms)
  [1m[35mLocation Load (0.0ms)[0m  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, MOD(CAST((ATAN2( ((locations.longitude - -97.7430608) / 57.2957795), ((locations.latitude - 30.267153) / 57.2957795)) * 57.2957795) + 360 AS decimal), 360) AS bearing FROM "locations" WHERE (locations.latitude BETWEEN 29.977689433778306 AND 30.556616566221695 AND locations.longitude BETWEEN -98.07821040202988 AND -97.40791119797011 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)))) BETWEEN 0.0 AND 20 AND locations.id != 65)  ORDER BY distance ASC
// Geocoding section complete
  [1m[36mPhotograph Exists (0.0ms)[0m  [1mSELECT  1 AS one FROM "photographs" WHERE "photographs"."location_id" = $1 LIMIT 1[0m  [["location_id", 65]]
  [1m[35mPhotograph Load (0.0ms)[0m  SELECT "photographs".* FROM "photographs" WHERE "photographs"."location_id" = $1  ORDER BY "photographs"."created_at" DESC  [["location_id", 65]]