我正在尝试获取给定的帖子记录的英里数...
class Post < ActiveRecord::Base
attr_accessor :lat, :lng
set_rgeo_factory_for_column(:location, RGeo::Geographic.spherical_factory(:srid => 4326))
def distance_from(current_location)
rgeo_factory.point(current_location[:lng], current_location[:lat]).distance(location) / 1.60934
end
private
def update_location!
self.location = rgeo_factory.point(lng, lat) if lat && lng
end
def rgeo_factory
Post.rgeo_factory_for_column(:location)
end
end
如果我创建一个位置为内华达州拉斯维加斯(纬度:36.255123,lng:-115.238348)的帖子,并且我验证了它——
1.9.3p125 :018 > p.lat = 36.255123
=> 36.255123
1.9.3p125 :019 > p.lng = -115.238348
=> -115.238348
1.9.3p125 :019 > p
#<Post id: 133, location: #<RGeo::Geographic::SphericalPointImpl:0x81d7130c "POINT (-115.238348 36.255123)">
当我试图获得那个职位和加利福尼亚某个地点之间的距离时,我得到了一个疯狂的英里数......
1.9.3p125 :027 > p.distance_from(:lat => 34.019454, :lng => -118.491191)
=> 240327.390598477
我究竟做错了什么?