我想在一个模型中对两个地址进行地理编码,即。一个旅程有一个StartAddress和一个EndAddress。因此,我的模型有三个属性: StartAddress start_longtitude start_lattitude EndAddress end_longtitude end_lattitude
我如何对其进行地理编码,以便调用nearbys函数来查找i)在给定旅程附近开始的旅程和ii)在特定旅程附近结束的旅程?任何帮助都将不胜感激
发布于 2014-10-23 11:00:21
最近的一个更新允许这样做。https://github.com/alexreisner/geocoder/pull/692
使用您自己的地理编码函数对模型进行地理编码,然后在near函数中指定您想要搜索的备用属性。
发布于 2016-09-17 20:26:54
here很好地表达了问题和解决方案。
将以下before_save和相应的方法添加到您的模型中,以解决此问题。不要忘记重复第二个位置(可能是目的地)的部分代码:
before_save :geocode_endpoints
private
#To enable Geocoder to works with multiple locations
def geocode_endpoints
if from_changed?
geocoded = Geocoder.search(loc1).first
if geocoded
self.latitude = geocoded.latitude
self.longitude = geocoded.longitude
end
end
# Repeat for destination
if to_changed?
geocoded = Geocoder.search(loc2).first
if geocoded
self.latitude2 = geocoded.latitude
self.longitude2 = geocoded.longitude
end
end
endhttps://stackoverflow.com/questions/26519483
复制相似问题