privateclass MyLocationListner implements LocationListener{
@Override
publicvoid onLocationChanged(Location location){// Called when a new location is found by the location provider.
Log.v("GPSTEST", "Got New Location of provider:"+location.getProvider());if(currentLocation!=null){if(isBetterLocation(location, currentLocation)){
Log.v("GPSTEST", "It's a better location");
currentLocation=location;
showLocation(location);}else{
Log.v("GPSTEST", "Not very good!");}}else{
Log.v("GPSTEST", "It's first location");
currentLocation=location;
showLocation(location);}//移除基于LocationManager.NETWORK_PROVIDER的监听器if(LocationManager.NETWORK_PROVIDER.equals(location.getProvider())){
locationManager.removeUpdates(this);}}//后3个方法此处不做处理publicvoid onStatusChanged(String provider, int status, Bundle extras){}publicvoid onProviderEnabled(String provider){}publicvoid onProviderDisabled(String provider){}};
Location currentLocation;privatevoid showLocation(Location location){//纬度
Log.v("GPSTEST","Latitude:"+location.getLatitude());//经度
Log.v("GPSTEST","Longitude:+location.getLongitude());
//精确度
Log.v("GPSTEST","Accuracy:"+location.getAccuracy());
//Location还有其它属性,请自行探索
}
privatestaticfinalint CHECK_INTERVAL =1000*30;protectedboolean isBetterLocation(Location location,
Location currentBestLocation){if(currentBestLocation ==null){// A new location is always better than no locationreturntrue;}// Check whether the new location fix is newer or olderlong timeDelta = location.getTime()- currentBestLocation.getTime();boolean isSignificantlyNewer = timeDelta > CHECK_INTERVAL;boolean isSignificantlyOlder = timeDelta <-CHECK_INTERVAL;boolean isNewer = timeDelta >0;// If it's been more than two minutes since the current location,// use the new location// because the user has likely movedif(isSignificantlyNewer){returntrue;// If the new location is more than two minutes older, it must// be worse}elseif(isSignificantlyOlder){returnfalse;}// Check whether the new location fix is more or less accurateint accuracyDelta =(int)(location.getAccuracy()- currentBestLocation
.getAccuracy());boolean isLessAccurate = accuracyDelta >0;boolean isMoreAccurate = accuracyDelta <0;boolean isSignificantlyLessAccurate = accuracyDelta >200;// Check if the old and new location are from the same providerboolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());// Determine location quality using a combination of timeliness and// accuracyif(isMoreAccurate){returntrue;}elseif(isNewer &&!isLessAccurate){returntrue;}elseif(isNewer &&!isSignificantlyLessAccurate
&& isFromSameProvider){returntrue;}returnfalse;}/** Checks whether two providers are the same */privateboolean isSameProvider(String provider1, String provider2){if(provider1 ==null){return provider2 ==null;}return provider1.equals(provider2);}