Skip to content

Commit 6e0f8ee

Browse files
committed
升级到2.0.10
1 parent 3e102a3 commit 6e0f8ee

8 files changed

Lines changed: 357 additions & 18 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<dependency>
1111
<groupId>com.javabaas</groupId>
1212
<artifactId>javasdk</artifactId>
13-
<version>2.0.9</version>
13+
<version>2.0.10</version>
1414
</dependency>
1515
</dependencies>
1616
```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.javabaas</groupId>
88
<artifactId>javasdk</artifactId>
9-
<version>2.0.9</version>
9+
<version>2.0.10</version>
1010
<packaging>jar</packaging>
1111
<name>JavaBaas_SDK_Java</name>
1212
<description>JavaBaas_SDK_Java</description>
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package com.javabaas.javasdk;
2+
3+
/**
4+
* Created by zangyilin on 2018/12/5.
5+
*/
6+
public class JBGeoPoint {
7+
static double EARTH_MEAN_RADIUS_KM = 6378.14D;
8+
static double ONE_KM_TO_MILES = 1.609344D;
9+
private double latitude;
10+
private double longitude;
11+
/**
12+
* 维度
13+
*/
14+
static final String LATITUDE_KEY = "latitude";
15+
/**
16+
* 经度
17+
*/
18+
static final String LONGTITUDE_KEY = "longitude";
19+
20+
public JBGeoPoint() {
21+
this.latitude = 0.0D;
22+
this.longitude = 0.0D;
23+
}
24+
25+
/**
26+
* 计算本位置到另一位置距离(公里)
27+
* @param point 位置
28+
* @return 距离
29+
*/
30+
public double distanceInKilometersTo(JBGeoPoint point) {
31+
float[] mResults = new float[2];
32+
computeDistanceAndBearing(this.latitude, this.longitude, point.latitude, point.longitude, mResults);
33+
34+
return mResults[0] / 1000.0F;
35+
}
36+
37+
/**
38+
* 计算本位置到另一位置距离(英里)
39+
* @param point 位置
40+
* @return 距离
41+
*/
42+
public double distanceInMilesTo(JBGeoPoint point) {
43+
return distanceInKilometersTo(point) / ONE_KM_TO_MILES;
44+
}
45+
46+
/**
47+
* 计算本位置到另一位置弧度
48+
*
49+
* @param point 位置
50+
* @return 弧度
51+
*/
52+
public double distanceInRadiansTo(JBGeoPoint point) {
53+
return distanceInKilometersTo(point) / EARTH_MEAN_RADIUS_KM;
54+
}
55+
56+
public JBGeoPoint(double latitude, double longitude) {
57+
this.latitude = latitude;
58+
this.longitude = longitude;
59+
}
60+
61+
public double getLatitude() {
62+
return this.latitude;
63+
}
64+
65+
public void setLatitude(double l) {
66+
this.latitude = l;
67+
}
68+
69+
public double getLongitude() {
70+
return this.longitude;
71+
}
72+
73+
public void setLongitude(double l) {
74+
this.longitude = l;
75+
}
76+
77+
private static void computeDistanceAndBearing(double lat1, double lon1, double lat2, double lon2, float[] results) {
78+
int MAXITERS = 20;
79+
80+
lat1 *= 0.017453292519943295D;
81+
lat2 *= 0.017453292519943295D;
82+
lon1 *= 0.017453292519943295D;
83+
lon2 *= 0.017453292519943295D;
84+
85+
double a = 6378137.0D;
86+
double b = 6356752.3142D;
87+
double f = (a - b) / a;
88+
double aSqMinusBSqOverBSq = (a * a - b * b) / (b * b);
89+
90+
double L = lon2 - lon1;
91+
double A = 0.0D;
92+
double U1 = Math.atan((1.0D - f) * Math.tan(lat1));
93+
double U2 = Math.atan((1.0D - f) * Math.tan(lat2));
94+
95+
double cosU1 = Math.cos(U1);
96+
double cosU2 = Math.cos(U2);
97+
double sinU1 = Math.sin(U1);
98+
double sinU2 = Math.sin(U2);
99+
double cosU1cosU2 = cosU1 * cosU2;
100+
double sinU1sinU2 = sinU1 * sinU2;
101+
102+
double sigma = 0.0D;
103+
double deltaSigma = 0.0D;
104+
double cosSqAlpha = 0.0D;
105+
double cos2SM = 0.0D;
106+
double cosSigma = 0.0D;
107+
double sinSigma = 0.0D;
108+
double cosLambda = 0.0D;
109+
double sinLambda = 0.0D;
110+
111+
double lambda = L;
112+
for (int iter = 0; iter < MAXITERS; iter++) {
113+
double lambdaOrig = lambda;
114+
cosLambda = Math.cos(lambda);
115+
sinLambda = Math.sin(lambda);
116+
double t1 = cosU2 * sinLambda;
117+
double t2 = cosU1 * sinU2 - sinU1 * cosU2 * cosLambda;
118+
double sinSqSigma = t1 * t1 + t2 * t2;
119+
sinSigma = Math.sqrt(sinSqSigma);
120+
cosSigma = sinU1sinU2 + cosU1cosU2 * cosLambda;
121+
sigma = Math.atan2(sinSigma, cosSigma);
122+
double sinAlpha = sinSigma == 0.0D ? 0.0D : cosU1cosU2 * sinLambda / sinSigma;
123+
cosSqAlpha = 1.0D - sinAlpha * sinAlpha;
124+
cos2SM = cosSqAlpha == 0.0D ? 0.0D : cosSigma - 2.0D * sinU1sinU2 / cosSqAlpha;
125+
126+
double uSquared = cosSqAlpha * aSqMinusBSqOverBSq;
127+
A = 1.0D + uSquared / 16384.0D * (4096.0D + uSquared * (-768.0D + uSquared * (320.0D - 175.0D * uSquared)));
128+
129+
double B = uSquared / 1024.0D * (256.0D + uSquared * (-128.0D + uSquared * (74.0D - 47.0D * uSquared)));
130+
131+
double C = f / 16.0D * cosSqAlpha * (4.0D + f * (4.0D - 3.0D * cosSqAlpha));
132+
double cos2SMSq = cos2SM * cos2SM;
133+
deltaSigma = B * sinSigma * (cos2SM + B / 4.0D * (cosSigma * (-1.0D + 2.0D * cos2SMSq) - B / 6.0D * cos2SM * (-3.0D + 4.0D * sinSigma * sinSigma) * (-3.0D + 4.0D * cos2SMSq)));
134+
135+
lambda = L + (1.0D - C) * f * sinAlpha * (sigma + C * sinSigma * (cos2SM + C * cosSigma * (-1.0D + 2.0D * cos2SM * cos2SM)));
136+
137+
double delta = (lambda - lambdaOrig) / lambda;
138+
if (Math.abs(delta) < 1.0E-12D) {
139+
break;
140+
}
141+
}
142+
float distance = (float)(b * A * (sigma - deltaSigma));
143+
results[0] = distance;
144+
if (results.length > 1) {
145+
float initialBearing = (float)Math.atan2(cosU2 * sinLambda, cosU1 * sinU2 - sinU1 * cosU2 * cosLambda);
146+
initialBearing = (float)(initialBearing * 57.29577951308232D);
147+
results[1] = initialBearing;
148+
if (results.length > 2) {
149+
float finalBearing = (float)Math.atan2(cosU1 * sinLambda, -sinU1 * cosU2 + cosU1 * sinU2 * cosLambda);
150+
finalBearing = (float)(finalBearing * 57.29577951308232D);
151+
results[2] = finalBearing;
152+
}
153+
}
154+
}
155+
}

src/main/java/com/javabaas/javasdk/JBObject.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,18 @@ private void updateSaveData(String key, Object value) {
191191
map.put("className", ((JBObject)value).getClassName());
192192
map.put("_id", ((JBObject)value).getObjectId());
193193
saveData.put(key, map);
194+
} else if (value instanceof JBGeoPoint) {
195+
List<Double> list = JBUtils.listFromGeoPoint((JBGeoPoint) value);
196+
saveData.put(key, list);
194197
} else {
195198
saveData.put(key, value);
196199
}
197200
}
198201

199202
private boolean checkKey(String key) {
200-
if (JBUtils.isEmpty(key))
203+
if (JBUtils.isEmpty(key)) {
201204
throw new IllegalArgumentException("字段为空");
205+
}
202206
if (key.startsWith("_")) {
203207
throw new IllegalArgumentException("字段不能以'_'开头");
204208
}
@@ -219,22 +223,26 @@ public String getString(String key) {
219223

220224
public int getInt(String key) {
221225
Number v = (Number) get(key);
222-
if (v != null)
226+
if (v != null) {
223227
return v.intValue();
228+
}
224229
return 0;
225230
}
226231

227232
public long getLong(String key) {
228233
Number number = (Number) get(key);
229-
if (number != null)
234+
if (number != null) {
230235
return number.longValue();
236+
237+
}
231238
return 0L;
232239
}
233240

234241
public double getDouble(String key) {
235242
Number number = (Number) get(key);
236-
if (number != null)
243+
if (number != null) {
237244
return number.doubleValue();
245+
}
238246
return 0;
239247
}
240248

@@ -243,6 +251,15 @@ public boolean getBoolean(String key) {
243251
return b == null ? false : b;
244252
}
245253

254+
public JBGeoPoint getJBGeoPoint(String key) {
255+
List<Double> list = getList(key);
256+
if (list.size() > 1) {
257+
return new JBGeoPoint(list.get(1), list.get(0));
258+
} else {
259+
return null;
260+
}
261+
}
262+
246263
public <T extends JBObject> T getJBObject(String key) {
247264
return (T) get(key);
248265
}

src/main/java/com/javabaas/javasdk/JBQuery.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,61 @@ public JBQuery<T> whereEndWith(String key, String suffix) {
463463
return this;
464464
}
465465

466+
/**
467+
* 方法先保留,最近要加上
468+
*/
469+
470+
// public JBQuery<T> whereNear(String key, JBGeoPoint point) {
471+
// conditions.whereNear(key, point);
472+
// return this;
473+
// }
474+
//
475+
// public JBQuery<T> whereWithinGeoBox(String key, JBGeoPoint southwest, JBGeoPoint northeast) {
476+
// conditions.whereWithinGeoBox(key, southwest, northeast);
477+
// return this;
478+
// }
479+
480+
/**
481+
* 查询附近X距离(单位米)内的数据
482+
*
483+
* @param key key
484+
* @param point 中心位置
485+
* @param maxDistance 最大半径 单位米
486+
* @return 结果
487+
*/
488+
public JBQuery<T> whereWithinKilometers(String key, JBGeoPoint point, double maxDistance) {
489+
conditions.whereWithinKilometers(key, point, maxDistance);
490+
return this;
491+
}
492+
493+
// public JBQuery<T> whereWithinKilometers(String key, JBGeoPoint point, double maxDistance,
494+
// double minDistance) {
495+
// conditions.whereWithinKilometers(key, point, maxDistance, minDistance);
496+
// return this;
497+
// }
498+
//
499+
// public JBQuery<T> whereWithinMiles(String key, JBGeoPoint point, double maxDistance) {
500+
// conditions.whereWithinMiles(key, point, maxDistance);
501+
// return this;
502+
// }
503+
//
504+
// public JBQuery<T> whereWithinMiles(String key, JBGeoPoint point, double maxDistance,
505+
// double minDistance) {
506+
// conditions.whereWithinMiles(key, point, maxDistance, minDistance);
507+
// return this;
508+
// }
509+
//
510+
// public JBQuery<T> whereWithinRadians(String key, JBGeoPoint point, double maxDistance) {
511+
// conditions.whereWithinRadians(key, point, maxDistance);
512+
// return this;
513+
// }
514+
//
515+
// public JBQuery<T> whereWithinRadians(String key, JBGeoPoint point, double maxDistance,
516+
// double minDistance) {
517+
// conditions.whereWithinRadians(key, point, maxDistance, minDistance);
518+
// return this;
519+
// }
520+
466521
/**
467522
* 查询字段匹配正则<br/>
468523
* "$regex"

0 commit comments

Comments
 (0)