我在Android Dev Guid中阅读了有关Obtaining User Location的教程,
我尝试将其改编为以下代码..但我不知道应该将哪个位置值放入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"
}
答案 0 :(得分:7)
真的不是那么难。代码的作用是接收到找到的位置的连续更新;您可以让多个听众收听不同的提供商,因此这些更新可能或多或少准确,具体取决于提供商(例如GPS可能比网络更准确)。 isBetterLocation(...)
评估监听器找到的位置实际上是否比您已经知道的位置更好(并且应该在您的代码中引用)。 isBetterLocation(...)代码已有详细记录,因此不难理解,但第一个参数位置是提供程序找到的新位置, currentBestLocation 是您已经了解的位置。
我使用的代码与您的代码大致相同,但我不只是采用最佳提供商。 处理程序的东西是因为我不想继续更新,只需在两分钟的最长时间内找到足够准确的最佳位置(GPS可能需要一点时间)。
private Location currentBestLocation = null;
private ServiceLocationListener gpsLocationListener;
private ServiceLocationListener networkLocationListener;
private ServiceLocationListener passiveLocationListener;
private LocationManager locationManager;
private Handler handler = new Handler();
public void fetchLocation() {
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
try {
LocationProvider gpsProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
LocationProvider networkProvider = locationManager.getProvider(LocationManager.NETWORK_PROVIDER);
LocationProvider passiveProvider = locationManager.getProvider(LocationManager.PASSIVE_PROVIDER);
//Figure out if we have a location somewhere that we can use as a current best location
if( gpsProvider != null ) {
Location lastKnownGPSLocation = locationManager.getLastKnownLocation(gpsProvider.getName());
if( isBetterLocation(lastKnownGPSLocation, currentBestLocation) )
currentBestLocation = lastKnownGPSLocation;
}
if( networkProvider != null ) {
Location lastKnownNetworkLocation = locationManager.getLastKnownLocation(networkProvider.getName());
if( isBetterLocation(lastKnownNetworkLocation, currentBestLocation) )
currentBestLocation = lastKnownNetworkLocation;
}
if( passiveProvider != null) {
Location lastKnownPassiveLocation = locationManager.getLastKnownLocation(passiveProvider.getName());
if( isBetterLocation(lastKnownPassiveLocation, currentBestLocation)) {
currentBestLocation = lastKnownPassiveLocation;
}
}
gpsLocationListener = new ServiceLocationListener();
networkLocationListener = new ServiceLocationListener();
passiveLocationListener = new ServiceLocationListener();
if(gpsProvider != null) {
locationManager.requestLocationUpdates(gpsProvider.getName(), 0l, 0.0f, gpsLocationListener);
}
if(networkProvider != null) {
locationManager.requestLocationUpdates(networkProvider.getName(), 0l, 0.0f, networkLocationListener);
}
if(passiveProvider != null) {
locationManager.requestLocationUpdates(passiveProvider.getName(), 0l, 0.0f, passiveLocationListener);
}
if(gpsProvider != null || networkProvider != null || passiveProvider != null) {
handler.postDelayed(timerRunnable, 2 * 60 * 1000);
} else {
handler.post(timerRunnable);
}
} catch (SecurityException se) {
finish();
}
}
private class ServiceLocationListener implements android.location.LocationListener {
@Override
public void onLocationChanged(Location newLocation) {
synchronized ( this ) {
if(isBetterLocation(newLocation, currentBestLocation)) {
currentBestLocation = newLocation;
if(currentBestLocation.hasAccuracy() && currentBestLocation.getAccuracy() <= 100) {
finish();
}
}
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {}
@Override
public void onProviderEnabled(String s) {}
@Override
public void onProviderDisabled(String s) {}
}
private synchronized void finish() {
handler.removeCallbacks(timerRunnable);
handler.post(timerRunnable);
}
/** Determines whether one Location reading is better than the current Location fix
* @param location The new Location that you want to evaluate
* @param currentBestLocation The current Location fix, to which you want to compare the new one
*/
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
//etc
}
private Runnable timerRunnable = new Runnable() {
@Override
public void run() {
Intent intent = new Intent(LocationService.this.getPackageName() + ".action.LOCATION_FOUND");
if(currentBestLocation != null) {
intent.putExtra(LocationManager.KEY_LOCATION_CHANGED, currentBestLocation);
locationManager.removeUpdates(gpsLocationListener);
locationManager.removeUpdates(networkLocationListener);
locationManager.removeUpdates(passiveLocationListener);
}
}
};