miechooy
miechooy

Reputation: 3422

Google Play Services - Location Xamarin Android

I have implements Google Play Services background location tracking in my app. I have used Location Manager before but it didn't work on some devices.

There is very weird thing while using device. Since I have released GooglePlayServices most of devices including mine sending location logs with big deviations.

There is map of my todays road: enter image description here

There are locations in database

//...
Latitude    Longitude   Provider    Accuracy
51,0994253  17,1077168  fused   21,5179996490479
51,0994253  17,1077168  fused   21,5179996490479
51,0996427  17,1076683  fused   21,7150001525879
51,0996427  17,1076683  fused   21,7150001525879
51,0996427  17,1076683  fused   21,7150001525879
51,1003416  17,1079516  fused   8
51,1003416  17,1079516  fused   8
51,1003416  17,1079516  fused   8
51,1008037  17,1083013  fused   8
51,1008037  17,1083013  fused   8
51,1008037  17,1083013  fused   8
51,0997649  17,0375168  fused   20               //Strange point
51,0997649  17,0375168  fused   20
51,0997649  17,0375168  fused   20
51,1094489  17,065886   fused   21,1340007781982
51,1094489  17,065886   fused   21,1340007781982
51,1094489  17,065886   fused   21,1340007781982
//....

As you can see strange location has accuracy equal to 20 and it seems to be very very strange.

Implementation:

public class GoogleApiLocationManager : Java.Lang.Object, IResultCallback, ILocationListener
{
    public GoogleApiLocationManager(Context context, GoogleApiManagerMode requestMode)
        {
            _requestMode = requestMode;
            _context = context;
            _client = new GoogleApiClient.Builder(context)
                .AddConnectionCallbacks(OnGoogleConnected)
                .AddOnConnectionFailedListener(OnGoogleConnectFailed)
                .AddApi(LocationServices.API)
                .Build();
            _client.Connect();
        }

         public void OnGoogleConnected()
        {
            switch (_requestMode)
            {
                case GoogleApiManagerMode.LastKnownLocation:
                    SaveLastKnownLocation();
                    break;
                case GoogleApiManagerMode.RequestNewLocation:
                    RunLocationRequest();
                    break;
            }
        }

        public void OnResult(Java.Lang.Object result)
        {
            var locationSettingsResult = (LocationSettingsResult)result;
            try
            {
                switch (locationSettingsResult.Status.StatusCode)
                {
                    case CommonStatusCodes.Success:
                        LocationServices.FusedLocationApi.RequestLocationUpdates(_client, _locationRequest, this);
                        break;
                    case CommonStatusCodes.ResolutionRequired:
                        Toast.MakeText(_context, "Could not get location. Please enable high accuracy in location settings", ToastLength.Short);
                        var intent = new Intent(Settings.ActionLocationSourceSettings);
                        intent.AddFlags(ActivityFlags.NewTask);
                        _context.StartActivity(intent);
                        break;
                    case LocationSettingsStatusCodes.SettingsChangeUnavailable:
                        //TODO:
                        break;
                }
            }
            catch (Exception e)
            {
                //TODO:
            }
        }

        private void SaveLastKnownLocation()
        {
           //Store to database
           _client.Disconnect();
        }

        private void RunLocationRequest()
        {
            _locationRequest = CreateLocationRequest();
            var builder = new           
                  LocationSettingsRequest.Builder().AddLocationRequest(_locationRequest);
            var pendingResult = LocationServices.SettingsApi.CheckLocationSettings(_client, builder.Build());
            pendingResult.SetResultCallback(this);
        }

         private LocationRequest CreateLocationRequest()
    {
        _locationRequest = new LocationRequest();
        _locationRequest.SetInterval((long)_triggerInterval.TotalMilliseconds);
        _locationRequest.SetFastestInterval((long)_triggerInterval.TotalMilliseconds / 2);
        _locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy);

        return _locationRequest;
    }
}

GoogleManager class is getting called every ~2 minutes but AlarmReciever which inherent from BroadcastReciver.

[BroadcastReceiver]
public class AlarmReciver : BroadcastReceiver
{
   //some login to wakeup each 2 minutes...

   //runs google api manager each period
    private void RunLocationManager()
        {
            new GoogleApiLocationManager(_context, GoogleApiManagerMode.LastKnownLocation);
            new GoogleApiLocationManager(_context, GoogleApiManagerMode.RequestNewLocation);
        }
}

Background location tracking is working fine but there are sometimes big deviations between location logs. However it was working really fine while using Location Manager which is depraciated.

Did anyone faced similar issue ?

EDIT: I went running and I found that every each time location goes to the same place. Here is screen: enter image description here

Upvotes: 3

Views: 689

Answers (1)

SushiHangover
SushiHangover

Reputation: 74174

When defining your LocationRequest if you do not request high accuracy then the fuse provider will use the lowest power option to return a location to you. This will usually be a wifi-only based location that is only as accurate as Google has mapped your location.

Of course the phone needs to have wifi, cellular, and GPS/location services turned on to provide the best results with the fused provider.

Add AccessFineLocation to your manifest permissions and set your LocationRequest priority to high accuracy to have the GPS powered up and sample your location and recheck your results.

LocationRequest _locationRequest = new LocationRequest()
    .SetPriority(LocationRequest.PriorityHighAccuracy);

Upvotes: 2

Related Questions