public static List<UserLocationResult> GetNear(double lon, double lat, int pageIndex, int pageSize,long Day,long StartTime) { double rangeInM = Convert.ToDouble( ZConfig.GetConfigString("Aotopddistance") ); List<UserLocationResult> objList = new List<UserLocationResult>(); try { // lat = 24.47871208190918, lon = 118.18478393554690 var options = DBManager.getGeoNearOption(rangeInM); var db = DBManager.getDB(); var collection = db.GetCollection(TableName); IMongoQuery query = Query<UserLocation>.Where(o =>o.Day== Day && o.StartTime< StartTime);// Query<UserLocation>.Where(o=>o.StartTime>=DateTime.Now.AddHours(8).AddHours(1) && o.Day.Date==DateTime.Now.Date);//<ObjectId>(t => t._id, 0); var req = collection.GeoNearAs<UserLocation>(query, lon, lat, pageSize * (pageIndex + 1), options).Hits.Skip(pageSize * pageIndex).Take(pageSize); if (req != null) { foreach (var item in req) { UserLocationResult objResult = new UserLocationResult(); objResult.Loc = item.Document; objResult.StartTime = item.Document.StartTime; objResult.EmployeeId = item.Document.EmployeeId; objResult.Day = item.Document.Day; objResult.BillNo = item.Document.BillNo; objResult.Distance = item.Distance; objList.Add(objResult); } } return objList; } catch (Exception exp) { NLogger.Error("查询附近的人:"+ exp.ToString()); return objList; } }
public class DBManager { public static double earthRadius = 6378137.0; // m public static MongoDatabase getDB() { string connectionStr = ZConfig.GetConfigString("MongoDB"); MongoClient client = new MongoClient(connectionStr); MongoServer server = client.GetServer(); MongoDatabase db = server.GetDatabase(ZConfig.GetConfigString("MongoDB_DBNAME") ); return db; } public static GeoNearOptionsBuilder getGeoNearOption(double rangeInM) { return GeoNearOptions.SetMaxDistance(rangeInM / earthRadius /* to radians */) .SetSpherical(true).SetDistanceMultiplier(earthRadius); } }