Rails Mongoid geo_near按距离排序

时间:2013-11-20 23:46:23

标签: ruby-on-rails geolocation mongoid geospatial rgeo

我要做的是按距离对mongoid geo_near(或within_circle)max_distance结果进行排序,因为我不知道为什么,但默认情况下不会这样做。

我的Rails gem文件中有mongoid_geospatial,mongoid_spacial和rgeo。我知道mongoid_spacial有这种能力,但我无法使用它,它导致了gmaps4rails的问题。

我正在尝试使用mongoid_geospatial(使用mongoid库)但没有成功,除了这个https://stackoverflow.com/questions/18633636/mongodb-aggregate-geonear-maxdistance之外我找不到任何资源,但我不知道如何将mongo转换为mongoid。

任何人都有过在mongoid中对geo_near或within_circle进行排序的经验吗?任何帮助将不胜感激。

我的控制器中的代码

      searchterm = session[:categoryid].to_s
      radius = session[:distance].to_f / 10       
      @places = Provider.all.where(:category.to_s => /.*#{searchterm}.*/)
      .geo_near([ session[:latitude].to_f,session[:longitude].to_f ]).max_distance(radius)    

      #@places = Provider.all.where(:category.to_s => /.*#{searchterm}.*/)
      #.within_circle(location: [[ session[:latitude].to_f,session[:longitude].to_f ], radius ])      
      #.sort(:servicescore.desc).sort(:pricescore.desc)

我的模型中的代码

include Mongoid::Document 
  include Mongoid::Timestamps
  include Mongoid::Paranoia
  include Mongoid::Geospatial
  include Mongoid::MultiParameterAttributes
  include Mongoid::Slug
  include Mongoid::Spacial::Document  
  include Gmaps4rails::ActsAsGmappable
  acts_as_gmappable :lat => 'latitude', :lon => 'longitude', :process_geocoding => true,
                    :check_process => :prevent_geocoding,
                    :address => "business_address"
  field :location, :type => Point
  spatial_index :location
  field :officialname
  field :business_description
  field :category#, :type => Array
  field :business_type
  field :tax_office
  field :tax_number
  field :pin  
  field :business_phone
  field :web_site
  field :business_address   
  field :latitude
  field :longitude

----------------------------------------------- --------------------------------

----------------------------------------------- --------------------------------

编辑问题

map

我几乎没有尝试任何相关的事情。阅读mongo和mongoid geo_near文档。它确实存储位置[lon,lat]顺序。我在保存提供者和中心位置时都尝试了[lon,lat]和[lat,long]。尝试了球形geo_near和within_circle。仍然没有成功。  Geo_near返回错误排序的结果,如上图和下面的列表所示。应该有一些我遗漏但找不到的东西。

红色部分是“geo_near_distance”输出结果,列表按照那些排序,但它们在地图上看错了。绿色计算是我一直在使用的jquery代码返回正确的距离(虽然不能在控制器中使用)。

应该有一些我想念的东西。谁能发现那个?

results

这是我的最新代码:

Provider.rb

  field :location, :type => Array  # [lat,lng]
  index({ location: "2d" }, { min: -180, max: 180 })
  spatial_index :location  

Providers_controller

  center = [session[:longitude].to_f, session[:latitude].to_f]
  radious = 100
  searchterm = session[:categoryid].to_s
  radius = session[:distance].to_f / 10     
  @places = Provider.all.where(:category.to_s => /.*#{searchterm}.*/)
  .geo_near(center).max_distance(radious)#.spherical
  #.near(location: center)
  #.within_circle( location: [center,radius] )
  #.sort(:geo_near_distance.desc)
  #.geo_near([ session[:latitude].to_f,session[:longitude].to_f ]).max_distance(100).spherical   
  #.unit("km")
  #.geo_near([ session[:latitude].to_f,session[:longitude].to_f ], max_distance: radius, unit: "km".to_sym, spherical: true).sort_by!{|r| r.geo[:distance] }
  #.geo_near([ session[:latitude].to_f,session[:longitude].to_f ]).max_distance(radius)   
  #.sort(:geo_near_distance)

绿色部分的JQuery

 var p1 = new LatLon(Geo.parseDMS($('#lat1_{{id}}').val()), Geo.parseDMS($('#lon1_{{id}}').val()));
    var p2 = new LatLon(Geo.parseDMS($('#lat2_{{id}}').val()), Geo.parseDMS($('#lon2_{{id}}').val()));
    var new_number = parseFloat(p1.distanceTo(p2)).toFixed(2);
    $('#result-distance_{{id}}').html(new_number+' km');

提供商数据

{ "_id" : ObjectId("5295ef0fdd5063ce1600002a"), "workdonecount" : 0, "pricescore" : 0, "servicescore" : 0, "_slugs" : [  "5295ef0fdd5063ce1600002a-1" ], "officialname" : "çeşme", "business_description" : "dsadsa", "category" : "5280ad334b315241af406c79", "business_type" : "Ticari", "tax_office" : "dads", "tax_number" : "dss", "pin" : "", "business_address" : "Balçova/İzmir, Türkiye", "latitude" : 38.3692939, "longitude" : 27.093442, "pink" : false, "hizmetkutusu_verified" : false, "user_id" : ObjectId("52946f54dd50636df6000002"), "location" : [  27.093441999999982,  38.3692939 ], "gmaps" : true, "updated_at" : ISODate("2013-11-27T13:09:36.214Z"), "created_at" : ISODate("2013-11-27T13:09:36.214Z") }
{ "_id" : ObjectId("5295f248dd5063ce16000030"), "workdonecount" : 0, "pricescore" : 0, "servicescore" : 0, "_slugs" : [  "5295f248dd5063ce16000030-1" ], "officialname" : "izmirdsads", "business_description" : "dsdsadsdsa", "category" : "5280ad334b315241af406c79", "business_type" : "Ticari", "tax_office" : "dasds", "tax_number" : "dadsa", "pin" : "", "business_address" : "Çeşme, Türkiye", "latitude" : 38.32980999999999, "longitude" : 26.3149209, "pink" : false, "hizmetkutusu_verified" : false, "user_id" : ObjectId("52946f54dd50636df6000002"), "location" : [  26.31492090000006,  38.32980999999999 ], "gmaps" : true, "updated_at" : ISODate("2013-11-27T13:23:20.423Z"), "created_at" : ISODate("2013-11-27T13:23:20.423Z") }
{ "_id" : ObjectId("5295f260dd5063ce16000032"), "workdonecount" : 0, "pricescore" : 0, "servicescore" : 0, "_slugs" : [  "5295f260dd5063ce16000032-1" ], "officialname" : "dsadsa", "business_description" : "dsadsa", "category" : "5280ad334b315241af406c79", "business_type" : "Ticari", "tax_office" : "dsadsa", "tax_number" : "dsads", "pin" : "", "business_address" : "Çiğli, 35580 İzmir, Türkiye", "latitude" : 38.496303, "longitude" : 27.0603911, "pink" : false, "hizmetkutusu_verified" : false, "user_id" : ObjectId("52946f54dd50636df6000002"), "location" : [  27.06039110000006,  38.496303 ], "gmaps" : true, "provider_image" : "gmaps1.jpg", "updated_at" : ISODate("2013-11-27T13:23:44.686Z"), "created_at" : ISODate("2013-11-27T13:23:44.686Z") }
{ "_id" : ObjectId("5295eee3dd5063ce16000026"), "workdonecount" : 0, "pricescore" : 0, "servicescore" : 0, "_slugs" : [  "5295eee3dd5063ce16000026-1" ], "officialname" : "dsdsa", "business_description" : "dadsa", "category" : "5280ad334b315241af406c79", "business_type" : "Ticari", "tax_office" : "dsa", "tax_number" : "dads", "pin" : "", "business_address" : "Buca, İzmir, Türkiye", "latitude" : 38.38813400000001, "longitude" : 27.1753358, "pink" : false, "hizmetkutusu_verified" : false, "user_id" : ObjectId("52946f54dd50636df6000002"), "location" : [  27.291124800000034,  38.3337361 ], "gmaps" : true, "updated_at" : ISODate("2013-11-27T13:08:51.544Z"), "created_at" : ISODate("2013-11-27T13:08:51.544Z") }
{ "_id" : ObjectId("5295eef5dd5063ce16000028"), "workdonecount" : 0, "pricescore" : 0, "servicescore" : 0, "_slugs" : [  "5295eef5dd5063ce16000028-1" ], "officialname" : "dsdsdsa", "business_description" : "dadsds", "category" : "5280ad334b315241af406c79", "business_type" : "Ticari", "tax_office" : "dasds", "tax_number" : "dadsa", "pin" : "", "business_address" : "Bornova, 35100 İzmir, Türkiye", "latitude" : 38.466414, "longitude" : 27.2192191, "pink" : false, "hizmetkutusu_verified" : false, "user_id" : ObjectId("52946f54dd50636df6000002"), "location" : [  27.219219100000032,  38.466414 ], "gmaps" : true, "updated_at" : ISODate("2013-11-27T13:09:10.122Z"), "created_at" : ISODate("2013-11-27T13:09:10.122Z") }

1 个答案:

答案 0 :(得分:3)

几个月前我用过它。这就是我使用它的方式:

class Person
  include Mongoid::Document
  field :location, :type => Array  # [lat,lng]
  index( { location: Mongo::GEO2D }, { min: -180, max: 180 })
end

然后你需要生成索引:

rake db:mongoid:create_indexes

然后你可以进行查询:

center = [ 1, 10 ]
radious = 100
Person.within_circle( location: [ center, radious ] )

或使用$ near query:

Person.geo_near(center).max_distance(radious)  # sorted by distance

所有信息均在此处指定:

祝你好运!