|
1 | | -package fr.free.nrw.commons.location; |
2 | | - |
3 | | -import android.location.Location; |
4 | | -import android.support.annotation.NonNull; |
5 | | - |
6 | | -public class LatLng { |
7 | | - |
8 | | - private final double latitude; |
9 | | - private final double longitude; |
10 | | - private final float accuracy; |
11 | | - |
12 | | - /** Accepts latitude and longitude. |
13 | | - * North and South values are cut off at 90° |
14 | | - * |
15 | | - * @param latitude double value |
16 | | - * @param longitude double value |
17 | | - */ |
18 | | - public LatLng(double latitude, double longitude, float accuracy) { |
19 | | - if (-180.0D <= longitude && longitude < 180.0D) { |
20 | | - this.longitude = longitude; |
21 | | - } else { |
22 | | - this.longitude = ((longitude - 180.0D) % 360.0D + 360.0D) % 360.0D - 180.0D; |
23 | | - } |
24 | | - this.latitude = Math.max(-90.0D, Math.min(90.0D, latitude)); |
25 | | - this.accuracy = accuracy; |
26 | | - } |
27 | | - |
28 | | - public static LatLng from(@NonNull Location location) { |
29 | | - return new LatLng(location.getLatitude(), location.getLongitude(), location.getAccuracy()); |
30 | | - } |
31 | | - |
32 | | - public int hashCode() { |
33 | | - boolean var1 = true; |
34 | | - byte var2 = 1; |
35 | | - long var3 = Double.doubleToLongBits(this.latitude); |
36 | | - int var5 = 31 * var2 + (int)(var3 ^ var3 >>> 32); |
37 | | - var3 = Double.doubleToLongBits(this.longitude); |
38 | | - var5 = 31 * var5 + (int)(var3 ^ var3 >>> 32); |
39 | | - return var5; |
40 | | - } |
41 | | - |
42 | | - public boolean equals(Object o) { |
43 | | - if (this == o) { |
44 | | - return true; |
45 | | - } else if (!(o instanceof LatLng)) { |
46 | | - return false; |
47 | | - } else { |
48 | | - LatLng var2 = (LatLng)o; |
49 | | - return Double.doubleToLongBits(this.latitude) == Double.doubleToLongBits(var2.latitude) && Double.doubleToLongBits(this.longitude) == Double.doubleToLongBits(var2.longitude); |
50 | | - } |
51 | | - } |
52 | | - |
53 | | - public String toString() { |
54 | | - return "lat/lng: (" + this.latitude + "," + this.longitude + ")"; |
55 | | - } |
56 | | - |
57 | | - /** |
58 | | - * Rounds the float to 4 digits and returns absolute value. |
59 | | - * |
60 | | - * @param coordinate A coordinate value as string. |
61 | | - * @return String of the rounded number. |
62 | | - */ |
63 | | - private String formatCoordinate(double coordinate) { |
64 | | - double roundedNumber = Math.round(coordinate * 10000d) / 10000d; |
65 | | - double absoluteNumber = Math.abs(roundedNumber); |
66 | | - return String.valueOf(absoluteNumber); |
67 | | - } |
68 | | - |
69 | | - /** |
70 | | - * Returns "N" or "S" depending on the latitude. |
71 | | - * |
72 | | - * @return "N" or "S". |
73 | | - */ |
74 | | - private String getNorthSouth() { |
75 | | - if (this.latitude < 0) { |
76 | | - return "S"; |
77 | | - } |
78 | | - |
79 | | - return "N"; |
80 | | - } |
81 | | - |
82 | | - /** |
83 | | - * Returns "E" or "W" depending on the longitude. |
84 | | - * |
85 | | - * @return "E" or "W". |
86 | | - */ |
87 | | - private String getEastWest() { |
88 | | - if (this.longitude >= 0 && this.longitude < 180) { |
89 | | - return "E"; |
90 | | - } |
91 | | - |
92 | | - return "W"; |
93 | | - } |
94 | | - |
95 | | - /** |
96 | | - * Returns a nicely formatted coordinate string. Used e.g. in |
97 | | - * the detail view. |
98 | | - * |
99 | | - * @return The formatted string. |
100 | | - */ |
101 | | - public String getPrettyCoordinateString() { |
102 | | - return formatCoordinate(this.latitude) + " " + this.getNorthSouth() + ", " |
103 | | - + formatCoordinate(this.longitude) + " " + this.getEastWest(); |
104 | | - } |
105 | | - |
106 | | - /** |
107 | | - * Return the location accuracy in meter. |
108 | | - * |
109 | | - * @return float |
110 | | - */ |
111 | | - public float getAccuracy() { |
112 | | - return accuracy; |
113 | | - } |
114 | | - |
115 | | - /** |
116 | | - * Return the longitude in degrees. |
117 | | - * |
118 | | - * @return double |
119 | | - */ |
120 | | - public double getLongitude() { |
121 | | - return longitude; |
122 | | - } |
123 | | - |
124 | | - /** |
125 | | - * Return the latitude in degrees. |
126 | | - * |
127 | | - * @return double |
128 | | - */ |
129 | | - public double getLatitude() { |
130 | | - return latitude; |
131 | | - } |
132 | | -} |
| 1 | +package fr.free.nrw.commons.location; |
| 2 | + |
| 3 | +import android.location.Location; |
| 4 | +import android.support.annotation.NonNull; |
| 5 | + |
| 6 | +/** |
| 7 | + * a latitude and longitude point with accuracy information, often of a picture |
| 8 | + */ |
| 9 | +public class LatLng { |
| 10 | + |
| 11 | + private final double latitude; |
| 12 | + private final double longitude; |
| 13 | + private final float accuracy; |
| 14 | + |
| 15 | + /** |
| 16 | + * Accepts latitude and longitude. |
| 17 | + * North and South values are cut off at 90° |
| 18 | + * |
| 19 | + * @param latitude the latitude |
| 20 | + * @param longitude the longitude |
| 21 | + * @param accuracy the accuracy |
| 22 | + * |
| 23 | + * Examples: |
| 24 | + * the Statue of Liberty is located at 40.69° N, 74.04° W |
| 25 | + * The Statue of Liberty could be constructed as LatLng(40.69, -74.04, 1.0) |
| 26 | + * where positive signifies north, east and negative signifies south, west. |
| 27 | + */ |
| 28 | + public LatLng(double latitude, double longitude, float accuracy) { |
| 29 | + if (-180.0D <= longitude && longitude < 180.0D) { |
| 30 | + this.longitude = longitude; |
| 31 | + } else { |
| 32 | + this.longitude = ((longitude - 180.0D) % 360.0D + 360.0D) % 360.0D - 180.0D; |
| 33 | + } |
| 34 | + this.latitude = Math.max(-90.0D, Math.min(90.0D, latitude)); |
| 35 | + this.accuracy = accuracy; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * gets the latitude and longitude of a given non-null location |
| 40 | + * @param location the non-null location of the user |
| 41 | + * @return |
| 42 | + */ |
| 43 | + public static LatLng from(@NonNull Location location) { |
| 44 | + return new LatLng(location.getLatitude(), location.getLongitude(), location.getAccuracy()); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * creates a hash code for the longitude and longitude |
| 49 | + */ |
| 50 | + public int hashCode() { |
| 51 | + boolean var1 = true; |
| 52 | + byte var2 = 1; |
| 53 | + long var3 = Double.doubleToLongBits(this.latitude); |
| 54 | + int var5 = 31 * var2 + (int)(var3 ^ var3 >>> 32); |
| 55 | + var3 = Double.doubleToLongBits(this.longitude); |
| 56 | + var5 = 31 * var5 + (int)(var3 ^ var3 >>> 32); |
| 57 | + return var5; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * checks for equality of two LatLng objects |
| 62 | + * @param o the second LatLng object |
| 63 | + */ |
| 64 | + public boolean equals(Object o) { |
| 65 | + if (this == o) { |
| 66 | + return true; |
| 67 | + } else if (!(o instanceof LatLng)) { |
| 68 | + return false; |
| 69 | + } else { |
| 70 | + LatLng var2 = (LatLng)o; |
| 71 | + return Double.doubleToLongBits(this.latitude) == Double.doubleToLongBits(var2.latitude) && Double.doubleToLongBits(this.longitude) == Double.doubleToLongBits(var2.longitude); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * returns a string representation of the latitude and longitude |
| 77 | + */ |
| 78 | + public String toString() { |
| 79 | + return "lat/lng: (" + this.latitude + "," + this.longitude + ")"; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Rounds the float to 4 digits and returns absolute value. |
| 84 | + * |
| 85 | + * @param coordinate A coordinate value as string. |
| 86 | + * @return String of the rounded number. |
| 87 | + */ |
| 88 | + private String formatCoordinate(double coordinate) { |
| 89 | + double roundedNumber = Math.round(coordinate * 10000d) / 10000d; |
| 90 | + double absoluteNumber = Math.abs(roundedNumber); |
| 91 | + return String.valueOf(absoluteNumber); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Returns "N" or "S" depending on the latitude. |
| 96 | + * |
| 97 | + * @return "N" or "S". |
| 98 | + */ |
| 99 | + private String getNorthSouth() { |
| 100 | + if (this.latitude < 0) { |
| 101 | + return "S"; |
| 102 | + } |
| 103 | + |
| 104 | + return "N"; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Returns "E" or "W" depending on the longitude. |
| 109 | + * |
| 110 | + * @return "E" or "W". |
| 111 | + */ |
| 112 | + private String getEastWest() { |
| 113 | + if (this.longitude >= 0 && this.longitude < 180) { |
| 114 | + return "E"; |
| 115 | + } |
| 116 | + |
| 117 | + return "W"; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Returns a nicely formatted coordinate string. Used e.g. in |
| 122 | + * the detail view. |
| 123 | + * |
| 124 | + * @return The formatted string. |
| 125 | + */ |
| 126 | + public String getPrettyCoordinateString() { |
| 127 | + return formatCoordinate(this.latitude) + " " + this.getNorthSouth() + ", " |
| 128 | + + formatCoordinate(this.longitude) + " " + this.getEastWest(); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Return the location accuracy in meter. |
| 133 | + * |
| 134 | + * @return float |
| 135 | + */ |
| 136 | + public float getAccuracy() { |
| 137 | + return accuracy; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Return the longitude in degrees. |
| 142 | + * |
| 143 | + * @return double |
| 144 | + */ |
| 145 | + public double getLongitude() { |
| 146 | + return longitude; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Return the latitude in degrees. |
| 151 | + * |
| 152 | + * @return double |
| 153 | + */ |
| 154 | + public double getLatitude() { |
| 155 | + return latitude; |
| 156 | + } |
| 157 | +} |
0 commit comments