簡體   English   中英

Geokit距離計算的奇數

[英]Geokit distance calculation oddity

我正在使用Geokit插件來計算current_user和其他用戶(實際上存儲在Profile模型中的地理編碼)之間的距離。

為了進行測試,我創建了兩個用戶,一個位於明尼蘇達州明尼阿波利斯,另一個位於明尼蘇達州聖保羅。 我使用Geokit gem在IRB會話中對“個人檔案”的經/緯對進行地理編碼。

我更改了索引視圖,以列出每個配置文件的名稱,位置和經/緯對。 這些值與數據庫中的值匹配。

我更改了索引視圖以顯示current_user的經/緯度對。 我以每個用戶的身份進行身份驗證,以確保current_user的經緯度對符合預期。 它做了。

我向Profile模型添加了一個名為ll的實例方法,以將lat / lng字段作為LatLng對象返回:

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的Index操作中更改了查詢,以計算每個用戶與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英里B(44.9444101,-93.0932742)-8.7 m

但是,當我通過用戶B身份驗證(44.9444101,-93.0932742)時,距離計算不正確:

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

我能夠驗證“原始”緯度/經度對之間的距離計算:

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

我不知所措解釋發生了什么。 任何想法,將不勝感激。

如果您這樣做會發生什么:

 @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

您的錯誤使我懷疑是否存在數據舍入/轉換問題,並且:origin應該能夠直接提取lat和lng。 您可能還想在調用Profile.find(...)之前檢查(logger.debug)所有涉及的類型。

我一直無法提出一個給出那些特定錯誤的簡單轉換,但這是我的懷疑。 如果那沒有幫助,您可以將生成的sql添加到輸出中嗎? 可能有可能據此找出出了什么問題。

編輯添加:

Craig,做類型調試,給出:

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查詢中正在顯示的內容,因為這將顯示字符串轉換數字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM