Skip to content

Commit 16c10e5

Browse files
authored
Conveniences to Position model (#309)
* add a Position.fromLngLat() method * add warning if lat or lon values seem out of range
1 parent 3d3ff75 commit 16c10e5

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

mapbox/app/src/main/java/com/mapbox/services/android/testapp/MainActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.mapbox.services.android.testapp.turf.TurfMidpointActivity;
3838
import com.mapbox.services.android.testapp.utils.MapMatchingActivity;
3939
import com.mapbox.services.android.testapp.utils.SimplifyPolylineActivity;
40+
import com.mapbox.services.commons.models.Position;
4041

4142
import java.util.ArrayList;
4243
import java.util.Arrays;
@@ -169,6 +170,10 @@ protected void onCreate(Bundle savedInstanceState) {
169170
Log.d(LOG_TAG, "MAS version name: " + BuildConfig.VERSION_NAME);
170171
Log.d(LOG_TAG, "MAS version code: " + BuildConfig.VERSION_CODE);
171172

173+
// Check Position warnings are working
174+
Log.d(LOG_TAG, "The following Position warnings are intentional:");
175+
Position.fromLngLat(185, 95);
176+
172177
// RecyclerView
173178
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
174179
recyclerView.setHasFixedSize(true);

mapbox/libjava-core/src/main/java/com/mapbox/services/Constants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
public class Constants {
66

7+
/**
8+
* Default locale
9+
*/
10+
public static final Locale DEFAULT_LOCALE = Locale.US;
11+
712
/**
813
* User agent for HTTP requests
914
*/

mapbox/libjava-core/src/main/java/com/mapbox/services/commons/models/Position.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
package com.mapbox.services.commons.models;
22

3+
import com.mapbox.services.Constants;
4+
5+
import java.util.logging.Logger;
6+
37
/**
48
* Represents a position defined by a longitude, latitude, and optionally, an altitude.
59
*
610
* @since 1.0.0
711
*/
812
public class Position {
913

14+
private static final Logger logger = Logger.getLogger(Position.class.getSimpleName());
15+
1016
private final double longitude;
1117
private final double latitude;
1218
private final double altitude;
1319

1420
/**
15-
* Private Constructor
21+
* Private constructor. It'll emit a warning if either latitude or longitude seem
22+
* to be out of range.
1623
*
1724
* @param longitude double value with position's longitude.
1825
* @param latitude double value with position's latitude.
@@ -23,6 +30,22 @@ private Position(double longitude, double latitude, double altitude) {
2330
this.longitude = longitude;
2431
this.latitude = latitude;
2532
this.altitude = altitude;
33+
34+
if (latitude < -90 || latitude > 90) {
35+
// Checks the latitude value is within range or provide a warning otherwise
36+
logger.warning(String.format(Constants.DEFAULT_LOCALE,
37+
"Latitude value seems to be out of range (found: %f, expected: [-90, 90]). "
38+
+ "Did you accidentally reverse the longitude/latitude order?",
39+
latitude));
40+
}
41+
42+
if (longitude < -180 || longitude > 180) {
43+
// Checks the longitude value is within range or provide a warning otherwise
44+
logger.warning(String.format(Constants.DEFAULT_LOCALE,
45+
"Longitude value seems to be out of range (found: %f, expected: [-180, 180]). "
46+
+ "Did you accidentally reverse the longitude/latitude order?",
47+
longitude));
48+
}
2649
}
2750

2851
/**
@@ -103,6 +126,20 @@ public static Position fromCoordinates(double[] coordinates) {
103126
}
104127
}
105128

129+
/**
130+
* Builds a {@link Position} from a double longitude and latitude. Identical to
131+
* {@link #fromCoordinates(double, double)} but more explicit about the right order
132+
* for the longitude and latitude parameters.
133+
*
134+
* @param longitude double longitude value.
135+
* @param latitude double latitude value.
136+
* @return {@link Position}.
137+
* @since 2.0.0
138+
*/
139+
public static Position fromLngLat(double longitude, double latitude) {
140+
return Position.fromCoordinates(longitude, latitude);
141+
}
142+
106143
/**
107144
* Checks if a position has an altitude value.
108145
*

0 commit comments

Comments
 (0)