Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Location.getAccuracy() returns negative accuracy

I've had the following crash happen in my app, once to me, and once in the wild. It happens when I draw a radius circle around the map and the accuracy is negative. I can't reproduce the issue though other than hard coding a negative value.

Seen on a Moto G gen1 (4.4.4) and Samsung Galaxy S3 (4.4.2)

Fatal Exception: java.lang.IllegalArgumentException: radius is negative
   at com.google.a.a.ae.a(Unknown Source)
   at com.google.maps.api.android.lib6.e.eg.(Unknown Source)
   at com.google.maps.api.android.lib6.e.ev.a(Unknown Source)
   at com.google.android.gms.maps.internal.j.onTransact(SourceFile:390)
   at android.os.Binder.transact(Binder.java:380)
   at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addCircle(Unknown Source)
   at com.google.android.gms.maps.GoogleMap.addCircle(Unknown Source)

The one time I saw it, I'd returned to the app in the morning and it would crash, pointing to something about the activity being destroyed and recreated?

The actual error goes through these three methods before the crash

@Override
public void onLocationChanged(Location location) {
    confirmValidLocationReading(location);
}

private void confirmValidLocationReading(Location location) {

    lastLocLL = new LatLng(location.getLatitude(), location.getLongitude());         
    setMapLocation(lastLocLL, location.getAccuracy());

}

private void setMapLocation(LatLng latLong, double accuracy) {

    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(latLong)      
            .zoom(16)                  
            .bearing(0)                
            .tilt(30)                   
            .build();                  

    googleMap.clear();
    googleMap.addMarker(new MarkerOptions().position(latLong));
    googleMap.setMyLocationEnabled(false);


    CircleOptions co = new CircleOptions();
    co.center(latLong);
    co.radius(accuracy);  //<--- Value is negative
    co.strokeColor(getResources().getColor(R.color.themePrimaryTwo));
    co.strokeWidth(4.0f);
    googleMap.addCircle(co);  //<--- App Crashes

    googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

}

What's confusing is I'm getting the value from the API with a negative accuracy, but the documentation says

"If this location does not have an accuracy, then 0.0 is returned. All locations generated by the LocationManager include an accuracy."

http://developer.android.com/reference/android/location/Location.html#getAccuracy()

Is there a reason this would return a negative value? What would a negative value mean if it's legit? Is it a bug in the location that google is providing me?

The app is running play-services-maps 7.0.0. I plan to be updating to a newer version but I'd still like to understand what went on here if it's not a bug in the location services api and I'll now put a check around drawing the circle...

like image 422
Ben987654 Avatar asked Sep 26 '26 02:09

Ben987654


1 Answers

If the API is really passing the negative value, then it should be a bug in the device's firmware.

You can't change the firmware, but you can avoid that by checking the accuracy:

if (accuracy < 0)
    accuracy = 0;

co.radius(accuracy);
like image 144
geNia Avatar answered Sep 28 '26 20:09

geNia