microg/GmsCore · error · IllegalArgumentException
invalid latitude:
Error message
invalid latitude:
What it means
Geofence.Builder throws IllegalArgumentException("invalid latitude: " + latitude) when checkLatLong receives a latitude outside the valid WGS-84 range [-90.0, 90.0]. The library validates coordinates at build time before any geofence can be registered.
Source
Thrown at play-services-location/core/system-api/src/main/java/android/location/Geofence.java:78
/** @hide */
public double getLongitude() {
return mLongitude;
}
/** @hide */
public float getRadius() {
return mRadius;
}
private static void checkRadius(float radius) {
if (radius <= 0) {
throw new IllegalArgumentException("invalid radius: " + radius);
}
}
private static void checkLatLong(double latitude, double longitude) {
if (latitude > 90.0 || latitude < -90.0) {
throw new IllegalArgumentException("invalid latitude: " + latitude);
}
if (longitude > 180.0 || longitude < -180.0) {
throw new IllegalArgumentException("invalid longitude: " + longitude);
}
}
private static void checkType(int type) {
if (type != TYPE_HORIZONTAL_CIRCLE) {
throw new IllegalArgumentException("invalid type: " + type);
}
}
public static final Parcelable.Creator<Geofence> CREATOR = new Parcelable.Creator<Geofence>() {
@Override
public Geofence createFromParcel(Parcel in) {
int type = in.readInt();
double latitude = in.readDouble();
double longitude = in.readDouble();View on GitHub (pinned to 157c9d86ac)
Solutions
- Swap lat/long if values were passed in the wrong order — |value| > 90 usually means a longitude was given as latitude
- Validate the coordinate is in [-90, 90] and finite before calling setCircularRegion
- Check the upstream data format: GeoJSON uses (lon, lat) ordering, most map SDKs use (lat, lng)
Example fix
// before
builder.setCircularRegion(lon, lat, radius); // swapped
// after
if (lat < -90 || lat > 90 || Double.isNaN(lat))
throw new IllegalArgumentException("latitude out of range: " + lat);
builder.setCircularRegion(lat, lon, radius); Defensive patterns
Strategy: validation
Validate before calling
public static boolean isValidLatitude(double lat) {
return Double.isFinite(lat) && lat >= -90.0 && lat <= 90.0;
} Try / catch
try {
Geofence g = new Geofence.Builder()
.setRequestId(id).setCircularRegion(lat, lon, radius).build();
} catch (IllegalArgumentException e) {
if (e.getMessage() != null && e.getMessage().startsWith("invalid latitude")) {
// likely swapped lat/long — check caller argument order
}
} Prevention
- Remember |lat| > 90 usually means a longitude was passed as latitude
- GeoJSON is (lon, lat); map SDKs are (lat, lon) — normalize your data pipeline
- Validate all coordinates on ingest, not at geofence build time
When it happens
Trigger: Calling setCircularRegion with latitude > 90, < -90, NaN, or Infinity while building the Geofence via Geofence.Builder.create().
Common situations: Swapped lat/long values (common when copying from (lng, lat) ordered sources like GeoJSON); uninitialized doubles; parsing coordinates from user input or APIs without bounds checks; unit/degrees-vs-radians mistakes.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- invalid radius:
- invalid longitude:
- Transition types not set.
- Non-negative loitering delay needs to be set when transition
- Expiration not set.
AI-assisted analysis of microg/GmsCore@157c9d86ac (2026-09-06).
Data as JSON: /api/errors/55a80d254a5636e4.
Report an issue: GitHub.