1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
package cgeo.geocaching.geopoint;
import cgeo.geocaching.Settings;
import cgeo.geocaching.utils.Log;
public class Viewport {
public final Geopoint center;
public final Geopoint bottomLeft;
public final Geopoint topRight;
public Viewport(final Geopoint bottomLeft, final Geopoint topRight) {
this.bottomLeft = bottomLeft;
this.topRight = topRight;
this.center = new Geopoint((bottomLeft.getLatitude() + topRight.getLatitude()) / 2,
(bottomLeft.getLongitude() + topRight.getLongitude()) / 2);
}
public Viewport(final Geopoint center, final double latSpan, final double lonSpan) {
this.center = center;
final double centerLat = center.getLatitude();
final double centerLon = center.getLongitude();
bottomLeft = new Geopoint(centerLat - latSpan / 2, centerLon - lonSpan / 2);
topRight = new Geopoint(centerLat + latSpan / 2, centerLon + lonSpan / 2);
}
public double getLatitudeMin() {
return bottomLeft.getLatitude();
}
public double getLatitudeMax() {
return topRight.getLatitude();
}
public double getLongitudeMin() {
return bottomLeft.getLongitude();
}
public double getLongitudeMax() {
return topRight.getLongitude();
}
public Geopoint getCenter() {
return center;
}
@Override
public String toString() {
return "(" + bottomLeft.toString() + "," + topRight.toString() + ")";
}
/**
* Check if coordinates are located in a viewport (defined by its center and span
* in each direction).
*
* @param centerLat
* the viewport center latitude
* @param centerLon
* the viewport center longitude
* @param spanLat
* the latitude span
* @param spanLon
* the longitude span
* @param coords
* the coordinates to check
* @return true if the coordinates are in the viewport
*/
public static boolean isCacheInViewPort(int centerLat, int centerLon, int spanLat, int spanLon, final Geopoint coords) {
return 2 * Math.abs(coords.getLatitudeE6() - centerLat) <= Math.abs(spanLat) &&
2 * Math.abs(coords.getLongitudeE6() - centerLon) <= Math.abs(spanLon);
}
/**
* Check if an area is located in a viewport (defined by its center and span
* in each direction).
*
* expects coordinates in E6 format
*
* @param centerLat1
* @param centerLon1
* @param centerLat2
* @param centerLon2
* @param spanLat1
* @param spanLon1
* @param spanLat2
* @param spanLon2
* @return
*/
public static boolean isInViewPort(int centerLat1, int centerLon1, int centerLat2, int centerLon2, int spanLat1, int spanLon1, int spanLat2, int spanLon2) {
try {
final int left1 = centerLat1 - (spanLat1 / 2);
final int left2 = centerLat2 - (spanLat2 / 2);
if (left2 <= left1) {
return false;
}
final int right1 = centerLat1 + (spanLat1 / 2);
final int right2 = centerLat2 + (spanLat2 / 2);
if (right2 >= right1) {
return false;
}
final int top1 = centerLon1 + (spanLon1 / 2);
final int top2 = centerLon2 + (spanLon2 / 2);
if (top2 >= top1) {
return false;
}
final int bottom1 = centerLon1 - (spanLon1 / 2);
final int bottom2 = centerLon2 - (spanLon2 / 2);
if (bottom2 <= bottom1) {
return false;
}
return true;
} catch (Exception e) {
Log.e(Settings.tag, "Viewport.isInViewPort: " + e.toString());
return false;
}
}
}
|