访问地理编码器 gem 范围

Accessing geocoder gem scopes

The gem manual表示Venue.near([40.71, -100.23], 20, :units => :km)

但是,除了单独的经度和纬度之外,此应用程序的模型对象还有两个地理编码坐标变量。为了测试各种搜索选项(postGIS、lon 和 lat 的数字索引,或调用地理编码器服务)是否有任何方法可以访问特定模型变量以查找给定位置附近的对象?

相关架构数据为:

t.decimal  "origin_lon",          precision: 15, scale: 10
t.decimal  "origin_lat",          precision: 15, scale: 10
t.spatial  "origin_lonlat",      limit: {:srid=>3857, :type=>"point"}
add_index "strappos", ["origin_lat"], :name => "index_strappos_on_origin_lat"
add_index "strappos", ["origin_lon"], :name => "index_strappos_on_origin_lon"
add_index "strappos", ["origin_lonlat"], :name => "index_strappos_on_origin_lonlat", :spatial => true

GeoCoder 默认使用 latitudelongitude 作为它们的变量名。由于您的不同,您需要添加一个额外的选项才能查询 origin_lonorigin_lat.

在您的模型中,为模型初始化 geocoder 时,您可以选择为 lat/lon 属性分配不同的名称:

# YourModel.rb
geocoded_by :address,
  latitude: :origin_lat, 
  longitude: :origin_lon

为了清楚起见,我将它放在多行中。如您所见,我告诉 geocoder 我的 lat/lon 属性名称与默认值不同。

现在,使用 near 查询模型将查看这些新属性名称而不是默认值。