我试图对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进行排序的经验?任何帮助将不胜感激。 我控制器中的代码:
在我的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
-------------------------------------------------------------------------------
编辑问题
我已经尝试了几乎所有相关的内容,但都没有成功。阅读了Mongo和Mongoid地理附近的文档,它确实评论了存储位置[lon,lat]的顺序。当保存提供者和中心位置时,我尝试过[lon,lat]和[lat,long]。也尝试了球形地理邻近和within_circle。但仍然没有成功。 如上图所示,geo_near返回我的结果排序不正确,并出现在列表下面。我应该遗漏了什么,但找不到它。
红色部分是“geo_near_distance”的输出结果,列表是根据这些排序的,但是它们是错误的,如地图上所示。绿色计算是我正在使用的jQuery代码,可以返回正确的距离(但无法在控制器中使用)。
我应该遗漏了什么。有谁能发现吗?
以下是我的最新代码:
Provider.rb
field :location, :type => Array # [lat,lng]
index({ location: "2d" }, { min: -180, max: 180 })
spatial_index :location
提供者控制器
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") }
geo_near_distance
属性?根据Mongoid V3 Doc Link,每个文档都应该有一个geo_near_distance
属性。 - Som Poddar