how to get the current user location without using a map in the activity? i just want to store the coordinates of the location ? i used this code but it doesn't works !
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
latitude = location.getLatitude();
longitude = location.getLongitude();
This is what I am using in my app, working fine :
public class TheLocationClass implements LocationListener {
final long MIN_TIME_INTERVAL = 60 * 1000L;
private boolean isGPSEnabled = false;
private boolean isNetworkEnabled = false;
private Location location; // location.getLatitude() & location.getLongitude() will give you what you need...
private LocationManager locationManager;
public TheLocationClass() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
public void requestForUpdates(){
if(locationManager == null) {
return;
}
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,TheLocationClass.this);
if (locationManager != null) {
Location tempLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (tempLocation != null && isBetterLocation(tempLocation,location))
location = tempLocation;
}
}
if (isGPSEnabled) {
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, TheLocationClass.this, null);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, TheLocationClass.this);
if (locationManager != null) {
Location tempLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (tempLocation != null && isBetterLocation(tempLocation,location))
location = tempLocation;
}
}
}
public void stopLocationUpdates(){
if(locationManager!=null){
locationManager.removeUpdates(TheLocationClass.this);
}
}
private boolean isBetterLocation(Location location, Location currentBestLocation) {
if (currentBestLocation == null) {
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > MIN_TIME_INTERVAL;
boolean isSignificantlyOlder = timeDelta < -MIN_TIME_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 moved
if (isSignificantlyNewer) {
return true;
} else if (isSignificantlyOlder) {
return false;
}
// Check whether the new location fix is more or less accurate
int 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 provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and accuracy
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 loc) {
if (loc != null && isBetterLocation(loc, location)) {
location = loc;
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
Just create instance of this class and call requestForUpdates().
Variable location has newly updated accurate location of device.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.