2010-05-19 166 views
0

我正在使用Geokit插件来计算current_user和其他用户之间的距离(实际存储在Profile模型中的地理编码)。Geokit距离计算奇数

为了测试目的,我创建了两个用户,一个在明尼阿波利斯,MN和一个在明尼苏达州圣保罗。我使用Geokit gem在IRB会话中对Profiles的lat/lng对进行地理编码。

我改变了索引视图来列出每个配置文件的名称,位置和纬度/经度对。这些值与数据库中的值匹配。

我改变了索引视图来显示current_user的纬度/经度对。我对每个用户进行身份验证,以确保current_user的lat/lng对符合预期。它做了。

我添加了一个实例方法的剖面模型,名为LL,返回纬度/经度领域经纬度对象:

def ll 
    #if lat and lng fields aren't empty 
    LatLng.new(self.lat,self.lng) if (!self.lat.nil? && !self.lng.nil?) 
end 

我改变了ProfileController可的索引操作的查询来计算每个用户之间的距离和CURRENT_USER:

@profiles = Profile.find(:all, 
    :conditions => conditions, # set WHERE clause 
    :order => order_by, # set ORDER BY clause 
    :origin => current_user.profile.ll, # calculate distance based on current_user's location 
    :units => :miles # use miles 

最后,我改变了索引视图来显示CURRENT_USER和每个单独的用户之间的距离:

<%=h profile.location %> (<%= profile.ll %>) - 
<%= profile.distance.to_f.round(1) %> mi 

当我认证为用户A(44.9799654,-93.2638361),则距离计算是正确的:

A(44.9799654,-93.2638361) - 0.0 MI B(44.9444101,-93.0932742) - 8.7米

然而,当我认证为用户B(44.9444101,-93.0932742),则距离计算是不正确的:

A(44.9799654,-93.2638361) - 32.8 MI B(44.9444101,-93.0932742) - 41.1 MI

我能够确认的“原始”纬度/经度对之间的距离计算:

a = LatLng.new(44.9799654,-93.2638361) 
=> #<Geokit::LatLng:0x1034af778 @lat=44.9799654, @lng=-93.2638361> 
>> b = LatLng.new(44.9444101,-93.0932742) 
=> #<Geokit::LatLng:0x1034aab88 @lat=44.9444101, @lng=-93.0932742> 
>> a.distance_to(b) 
=> 8.70261379563918 
>> b.distance_to(a) 
=> 8.70261379563918 

我在无法解释发生了什么。任何想法,将不胜感激。

回答

1

如果你这样做,会发生什么:

@profiles = Profile.find(:all, 
    :conditions => conditions, # set WHERE clause 
    :order => order_by, # set ORDER BY clause 
    :origin => current_user.profile, # .ll # calculate distance based on current_user's location 
    :units => :miles # use miles 

你的错误让我怀疑是不是有持续的数据四舍五入/转换问题和:起源应该能够直接拿起lat和液化天然气。您可能还想在调用Profile.find(...)前检查(logger.debug)所有类型,然后调用Profile.find(...)

我还没有能够想出一个简单的转换来提供这些特定的错误,但这是我的怀疑。如果这没有帮助,你可以将生成的sql添加到输出中吗?基于这一点,有可能弄清楚出了什么问题。

编辑补充:

克雷格,做调试开关,给:

logger.debug("lat: #{current_user.profile.lat} #{current_user.profile.lat.class.to_s} lng: #{current_user.profile.lng} #{current_user.profile.lng.class.to_s}") 
logger.debug("lat: #{current_user.profile.ll.lat} #{current_user.profile.ll.lat.class.to_s} lng: #{current_user.profile.ll.lng} #{current_user.profile.ll.lng.class.to_s}") 

当你真正遇到的错误,然后看看你的development.log一试。 (注意:这个代码中可能存在一个错误,因为我只是将它输入到窗口中,但你明白了。)否则,你可能应该查看SQL查询中显示的内容,因为它会显示post-字符串转换号码。

+0

谢谢!谢谢!谢谢!这似乎是一个转换问题,但如果我能确定它是什么,我会被诅咒的。我想看看类型(logger.debug)和生成的sql。你能告诉我该怎么做吗? – craig 2010-05-21 02:11:57