0

I implemented the geocoder gem based on the tutorial on http://railscasts.com/episodes/273-geocoder

This is a part of the house show view template

- @house.location.nearbys(20, {:order => :distance, :units => :km}).each do |house|
   %li
    = link_to house.name, house_path
    (#{city.distance.round(2)} KM)

This show al other houses with the distance in KM, so this works fine.

I already showed on the same house page all the cities of the region where the house is located, like this:

%ul.tags
 - @region.cities.each do |city|
   %li= link_to city.name, country_region_city_path(@country, @region, city)

Question: How can i implement the geocoder gem in the @region.cities loop so the visitor can see on the house page how many KM is it to drive from the house to the cities?

Thanks...remco

4

2 回答 2

1

地理编码对象应该有一个distance_from方法。 @house如果您遵循 Ryan Bates 的教程,应该有这种方法。

检查文档以了解如何使用它: https ://github.com/alexreisner/geocoder#location-aware-database-queries

你基本上只需要问@house它与城市的距离(在你的城市圈内)

于 2013-05-10T00:57:16.967 回答
0

是的,我做到了……这并没有我想象的那么困难……

  - @region.cities.each do |city|
     %li
      = link_to city.name, country_region_city_path(@country, @region, city)
     %li 
      = @house.location.distance_from([city.latitude,city.longitude]) 
于 2013-05-10T13:01:03.240 回答