microg/GmsCore · error · IllegalArgumentException

invalid longitude:

Error message

invalid longitude: 

What it means

Range validation in Geofence.checkLatLong: the longitude is outside [-180.0, 180.0], so the geofence center is invalid. The offending value is appended; applied when constructing a Geofence from builder input or parcel data.

Source

Thrown at play-services-location/core/system-api/src/main/java/android/location/Geofence.java:81

    }

    /** @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();
            float radius = in.readFloat();
            checkType(type);
            return Geofence.createCircle(latitude, longitude, radius);

View on GitHub (pinned to 157c9d86ac)

Solutions

  1. Normalize the longitude into [-180, 180] (e.g. ((lon + 540) % 360) - 180) before building the geofence
  2. Convert radians to degrees if the source uses radians (Math.toDegrees)
  3. Validate the value is finite and within range before calling setCircularRegion

Example fix

// before
builder.setCircularRegion(lat, lonRadians, radius);
// after
double lonDeg = Math.toDegrees(lonRadians);
lonDeg = ((lonDeg + 540.0) % 360.0) - 180.0; // normalize to [-180,180]
builder.setCircularRegion(lat, lonDeg, radius);
Defensive patterns

Strategy: validation

Validate before calling

public static double normalizeLongitude(double lon) {
    return ((lon + 540.0) % 360.0 + 360.0) % 360.0 - 180.0;
}
// call normalizeLongitude before setCircularRegion

Type guard

public static boolean isValidLongitude(double lon) {
    return Double.isFinite(lon) && lon >= -180.0 && lon <= 180.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 longitude")) {
        // normalize or convert radians before retrying
    }
}

Prevention

When it happens

Trigger: Calling setCircularRegion with longitude > 180, < -180, NaN, or Infinity (e.g. values in radians, or cumulative heading values exceeding a full circle) before create().

Common situations: Passing radians instead of degrees; not normalizing longitudes that wrap around the antimeridian (e.g. 190 instead of -170); uninitialized or misparsed coordinate fields; swapped/offset column parsing.

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


AI-assisted analysis of microg/GmsCore@157c9d86ac (2026-09-06). Data as JSON: /api/errors/996160b56fcb9645. Report an issue: GitHub.