Using Google Play Services' FusedLocationApi

Hi everyone,

For some time now, I've converted some old code in my Application class from using the old native location tracking API to the new Google Play Services FusedLocationAPI (which seems to be much faster at finding a user's initial location). It works great on my test devices (S3, S4) but I'm seeing a lot of crashes when connecting and disconnecting from the FusedLocationAPI when the app is out in the wild. Can anyone tell me best practices for using the FusedLocationAPI? I've included the relevant chunk of the Application class and a typical example of the exceptions being thrown (it looks like the GoogleApiClient is failing).

[Application]
public class MyApp : Application, IGoogleApiClientConnectionCallbacks, IGoogleApiClientOnConnectionFailedListener, Android.Gms.Location.ILocationListener
{
    IGoogleApiClient googleClient;
    LocationRequest locReq;

    public MyApp(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
    }

    public override void OnCreate()
    {
        base.OnCreate();
    }

    public void OnConnected(Bundle bundle)
    {
        locReq = LocationRequest.Create();
        locReq.SetPriority(100);
        locReq.SetFastestInterval(500);
        locReq.SetInterval(1000);

        if (googleClient != null)
        {
            LocationServices.FusedLocationApi.RequestLocationUpdates(googleClient, locReq, this);
            Location lastLocation = LocationServices.FusedLocationApi.GetLastLocation(googleClient);
            if (lastLocation != null)
                CaptureLocation(lastLocation);
        }
    }
    public void OnLocationChanged(Location loc)
    {
        CaptureLocation(loc);
    }
    public void OnDisconnected(Bundle bundle)
    {
    }
    public void OnConnectionFailed(ConnectionResult result)
    {
    }
    public void OnConnectionSuspended(int i)
    {
    }

    public void StartGPS()
    {
        googleClient = new GoogleApiClientBuilder(this)
            .AddApi(LocationServices.Api)
            .AddConnectionCallbacks(this)
            .AddOnConnectionFailedListener(this)
            .Build();
        googleClient.Connect();
    }
    public void StopGPS()
    {
        if (googleClient != null)
        {
            LocationServices.FusedLocationApi.RemoveLocationUpdates(googleClient, this);
            googleClient.Disconnect();
            googleClient = null;
        }
    }
    public void CaptureLocation(Location loc)
    {
        if (loc != null)
        {

        }
    }
}

Exceptions:

0   Java.Lang.RuntimeException: Exception of type 'Java.Lang.RuntimeException' was thrown.
1   at .Java.Lang.RuntimeException : Exception of type 'Java.Lang.RuntimeException' was thrown.()
2   at .at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <0x00028>()
3   at .at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr,Android.Runtime.JValue*) <0x000c3>()
4   at .at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr,Android.Runtime.JValue[]) <0x0005f>()
5   at .at Android.Gms.Location.IFusedLocationProviderApiInvoker.RemoveLocationUpdates (Android.Gms.Common.Apis.IGoogleApiClient,Android.Gms.Location.ILocationListener) <0x0013b>()
6   at .at App1.MyApp.StopGPS () <0x0003b>()

0   Java.Lang.RuntimeException: Exception of type 'Java.Lang.RuntimeException' was thrown.
1   at .Java.Lang.RuntimeException : Exception of type 'Java.Lang.RuntimeException' was thrown.()
2   at .at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <0x00028>()
3   at .at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr,Android.Runtime.JValue*) <0x000c3>()
4   at .at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr,Android.Runtime.JValue[]) <0x0005f>()
5   at .at Android.Gms.Location.IFusedLocationProviderApiInvoker.RequestLocationUpdates (Android.Gms.Common.Apis.IGoogleApiClient,Android.Gms.Location.LocationRequest,Android.Gms.Location.ILocationListener) <0x001a3>()
6   at .at App1.MyApp.OnConnected (Android.OS.Bundle) <0x000a3>()
7   at .at Android.Gms.Common.Apis.IGoogleApiClientConnectionCallbacksInvoker.n_OnConnected_Landroid_os_Bundle_ (intptr,intptr,intptr) <0x0005f>()
8   at .at (wrapper dynamic-method) object.00f48c74-2de5-44bf-a9bf-032fd02f1e6e (intptr,intptr,intptr) <0x00043>()
9   at .()
10  at . --- End of managed exception stack trace ---()
11  at .java.lang.IllegalStateException: GoogleApiClient is not connected yet.()

Posts

Sign In or Register to comment.