Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Android
public class LocationOptimized implements LocationListener {
 
	public interface RegisteSingleLocationUpdate {
		public void onLocationUpdateReceived(Location newLocation);
	}
 
	private static final int TWO_MINUTES = 1000 * 60 * 2;
 
	private RegisteSingleLocationUpdate locationUpdater;
	private LocationManager mLocationManager;
	private Location currentLocation;
 
	public LocationOptimized(Context context) {
		mLocationManager = (LocationManager) context
				.getSystemService(Context.LOCATION_SERVICE);
 
		currentLocation = mLocationManager
				.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		Location newLocation = mLocationManager
				.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
 
		boolean best = isBetterLocation(newLocation, currentLocation);
		if (best) {
			currentLocation = newLocation;
		}
	}
 
	public Location getBestLocationAvailable() {
		return currentLocation;
	}
 
	public void checkForANewBestLocation(
			RegisteSingleLocationUpdate locationUpdater) {
		this.locationUpdater = locationUpdater;
 
		mLocationManager.requestLocationUpdates(
				LocationManager.NETWORK_PROVIDER, 0, 0, this);
 
		mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				0, 0, this);
	}
 
	protected boolean isBetterLocation(Location location,
			Location currentBestLocation) {
		if (currentBestLocation == null) {
			return true;
		}
 
		long timeDelta = location.getTime() - currentBestLocation.getTime();
		boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
		boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
		boolean isNewer = timeDelta > 0;
 
		if (isSignificantlyNewer) {
			return true;
		} else if (isSignificantlyOlder) {
			return false;
		}
 
		int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation
				.getAccuracy());
		boolean isLessAccurate = accuracyDelta > 0;
		boolean isMoreAccurate = accuracyDelta < 0;
		boolean isSignificantlyLessAccurate = accuracyDelta > 200;
 
		boolean isFromSameProvider = isSameProvider(location.getProvider(),
				currentBestLocation.getProvider());
 
		if (isMoreAccurate) {
			return true;
		} else if (isNewer && !isLessAccurate) {
			return true;
		} else if (isNewer && !isSignificantlyLessAccurate
				&& isFromSameProvider) {
			return true;
		}
		return false;
	}
 
	private boolean isSameProvider(String provider1, String provider2) {
		if (provider1 == null) {
			return provider2 == null;
		}
		return provider1.equals(provider2);
	}
 
	@Override
	public void onLocationChanged(Location newBestLocation) {
		
		System.out.println("On Location Change Call.......");
		mLocationManager.removeUpdates(this);
		boolean best = isBetterLocation(newBestLocation, currentLocation);
		if (best) {
			currentLocation = newBestLocation;
		}
		locationUpdater.onLocationUpdateReceived(currentLocation);
	}
 
	@Override
	public void onProviderDisabled(String arg0) {
	}
 
	@Override
	public void onProviderEnabled(String arg0) {
	}
 
	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
	}
}
 

public class MainActivity extends Activity implements RegisteSingleLocationUpdate{
	
	private LocationResult result;	
	LocationOptimized mylocation;
	String strLatitude = "", strLongitude = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		mylocation = new LocationOptimized(MainActivity.this);
		mylocation.checkForANewBestLocation(MainActivity.this);
	}
 
	
	@Override
	public void onLocationUpdateReceived(Location newLocation) {
 
		System.out.println("In Location Update Received : ");
		if (newLocation == null) {
 
			Location location = mylocation.getBestLocationAvailable();
			strLatitude = String.format("%.5f", location.getLatitude());
			strLongitude = String.format("%.5f", location.getLongitude());
		
		} else {
			strLatitude = String.format("%.6f", newLocation.getLatitude());
			strLongitude = String.format("%.6f", newLocation.getLongitude());
		}
	}
}
 
Above is My Code For Geting Current Lat Long,Code Perfect Work in All Mobile but Problem arise in Gelexy s3.
when i run my code in gelexy s3 current location not find ,so i debug code ,i found OnLocationChange Method in LocationOptimized class is Not Execute ,so That i get Location Null.
Please tell me if You have any Solution.my Quetion is why OnLocationChange method not Call?
Posted 18-Jun-13 22:23pm
Edited 4-Jul-13 17:14pm
(no name)41.5K
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 329
2 BillWoodruff 210
3 Afzaal Ahmad Zeeshan 204
4 CPallini 185
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 4 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100