Merge pull request #130 from onerain88/geo-query
feat: support geo query
commit
93310c9810
|
@ -1,4 +1,6 @@
|
|||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using LeanCloud.Storage;
|
||||
|
||||
namespace Storage.Test {
|
||||
|
@ -19,5 +21,32 @@ namespace Storage.Test {
|
|||
TestContext.WriteLine(radians);
|
||||
Assert.Less(radians - 0.0005, 0.0001);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Query() {
|
||||
LCObject geoObj = new LCObject("GeoObj");
|
||||
Random random = new Random();
|
||||
LCGeoPoint p1 = new LCGeoPoint(-90 + random.NextDouble() * 180, -180 + random.NextDouble() * 360);
|
||||
geoObj["location"] = p1;
|
||||
await geoObj.Save();
|
||||
|
||||
LCGeoPoint p2 = new LCGeoPoint(p1.Latitude + 0.01, p1.Longitude + 0.01);
|
||||
|
||||
double km = p1.KilometersTo(p2);
|
||||
TestContext.WriteLine($"km: {km}, {Math.Ceiling(km)}");
|
||||
LCQuery<LCObject> query = new LCQuery<LCObject>("GeoObj");
|
||||
query.WhereWithinKilometers("location", p2, Math.Ceiling(km));
|
||||
Assert.Greater((await query.Find()).Count, 0);
|
||||
|
||||
double miles = p1.MilesTo(p2);
|
||||
query = new LCQuery<LCObject>("GeoObj");
|
||||
query.WhereWithinMiles("location", p2, Math.Ceiling(miles));
|
||||
Assert.Greater((await query.Find()).Count, 0);
|
||||
|
||||
double radians = p1.RadiansTo(p2);
|
||||
query = new LCQuery<LCObject>("GeoObj");
|
||||
query.WhereWithinRadians("location", p2, Math.Ceiling(radians));
|
||||
Assert.Greater((await query.Find()).Count, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -94,6 +94,14 @@ namespace LeanCloud.Storage.Internal.Query {
|
|||
AddOperation(key, "$within", value);
|
||||
}
|
||||
|
||||
public void WhereWithinRadians(string key, LCGeoPoint point, double maxDistance) {
|
||||
Dictionary<string, object> value = new Dictionary<string, object> {
|
||||
{ "$nearSphere", point },
|
||||
{ "$maxDistance", maxDistance }
|
||||
};
|
||||
Add(new LCEqualCondition(key, value));
|
||||
}
|
||||
|
||||
public void WhereRelatedTo(LCObject parent, string key) {
|
||||
Add(new LCRelatedCondition(parent, key));
|
||||
}
|
||||
|
|
|
@ -205,6 +205,19 @@ namespace LeanCloud.Storage {
|
|||
return this;
|
||||
}
|
||||
|
||||
public LCQuery<T> WhereWithinRadians(string key, LCGeoPoint point, double maxDistance) {
|
||||
Condition.WhereWithinRadians(key, point, maxDistance);
|
||||
return this;
|
||||
}
|
||||
|
||||
public LCQuery<T> WhereWithinMiles(string key, LCGeoPoint point, double maxDistance) {
|
||||
return WhereWithinRadians(key, point, maxDistance / 3958.8);
|
||||
}
|
||||
|
||||
public LCQuery<T> WhereWithinKilometers(string key, LCGeoPoint point, double maxDistance) {
|
||||
return WhereWithinRadians(key, point, maxDistance / 6371.0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The value corresponding to key is related to the parent.
|
||||
/// </summary>
|
||||
|
|
Loading…
Reference in New Issue