I've been experimenting with the Android SDK for a few weeks now, trying to achieve an accurate location from a background service.
After trying a few configurations, I currently have this on a loop:
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(true);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
bestProvider = locationManager.getBestProvider(criteria, true);
lastKnownLocation = locationManager.getLastKnownLocation(bestProvider);
And then I check lastKnownLocation
every now and then for a position update. I know you can listen for updates but at the moment I'm not too concerned about that right now. What I am concerned about is, (I think) I'm asking for the phone to use GPS whenever possible - instead of other methods of determining the location - an yet it still returns a latitude / longitude from a good distance away, yet when I open the maps application, it has me within a couple of meters.
Can anyone suggest where I'm going wrong here?
I have used the below code to get accurate location. Using below code you can handle enabling/disabling the GPS programmatically.
to disable the GPS, simply pass false for flag value of ToggleGPS() method. Hope this helps you.
Setting the Criteria just establishes which provider is best to use depending on them so that doesn't really have a say on the accuracy or the validity of the location. I just set the provider to GPS straight away (If GPS is available!).
Also it doesn't seem like your giving it any requirements concerning how long you want to wait before updating based on time and distance. Here is an example of what I do using intents and broadcast receiver. It may help you.
And then in the same class I put my
broadcast receiver
The action for my intent filter is just a static reference to a constant set in my activity.
This worked in my case in providing me with accurate locations. You can set the distance to 0 if you want then what you will find is you get location fixes of an accuracy of 5 every second if you have a good fix of 4 satellites or more.
I hope this helps you