I read the tutorial about Obtaining User Location in Android Dev Guid and,
I try to adapt this to the following code.. but i don't know which location value I should put into isBetterLocation(Location location, Location currentBestLocation)
Example.class
private LocationManager locman;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String context = Context.LOCATION_SERVICE;
locman = (LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locman.getBestProvider(criteria, true);
locman.requestLocationUpdates(
provider,MIN_TIME, MIN_DISTANCE, locationListener);
}
private LocationListener locationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location) {
// What should i pass as first and second parameter in this method
if(isBetterLocation(location1,location2)){
// isBetterLocation = true > do updateLocation
updateLocation(location);
}
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}
//Brief ... See code in Android Dev Guid "Obtaining User Location"
}
Its not that hard really. What the code does is it receives continuous updates on locations found; you can have multiple listeners listening to different providers and as such those updates can be more or less accurate depending on the provider (GPS for example could be more accurate than network).
isBetterLocation(...)
evaluates if a location found by the listener is actually better than the one you already know about (and should have a reference to in your code). The isBetterLocation(...) code is well documented, so it shouldn't be hard to understand, but the first parameter location is the new location found by a provider, and currentBestLocation is the location you already know about.The code I use is about the same as yours, except I don't just take best provider. The handler stuff is because I don't want continued updates, just find the best possible location that is accurate enough for me within a maximum timeframe of two minutes (GPS can take a bit).