简体   繁体   English

在嵌套资源循环中实现gecoder gem?

[英]Implement the gecoder gem in nested resource loop?

I implemented the geocoder gem based on the tutorial on http://railscasts.com/episodes/273-geocoder 我根据http://railscasts.com/episodes/273-geocoder上的教程实现了geocoder gem

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. 此图显示了其他房屋的距离(以KM为单位),因此效果很好。

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? 问题:如何在@ region.cities循环中实现地址解析器gem,以便访问者可以在房屋页面上看到从房屋开车到城市要多少公里?

Thanks...remco 谢谢...雷姆科

Geocoded objects should have a distance_from method. 经过地理编码的对象应具有distance_from方法。 @house should have that method if you follow Ryan Bates' tutorial. 如果遵循Ryan Bates的教程, @house应该具有该方法。

Check the docs for how to use it: https://github.com/alexreisner/geocoder#location-aware-database-queries 检查文档以了解如何使用它: https : //github.com/alexreisner/geocoder#location-aware-database-queries

You basically just need to ask @house it's distance from the city (within your cities loop) 您基本上只需要询问@house它与城市的距离(在您的城市圈内)

yep i did it...it was not so dificult i expected... 是的,我做到了。。。

  - @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]) 

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

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