Reputation: 75
My code putput always goes in else part. IT means {location} is null.
Any suggestions?
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, locationListener);
location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null)
{
double latitude = location.getLatitude();
double longitude = location.getLongitude();
} else {
//
}
.....
.....
....
Upvotes: 0
Views: 243
Reputation: 71
If locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) returns null then it means you have never been located.
If you want to get the new location, you should make you activity extend the LocationListener interface and implement the following.
public void onLocationChanged(Location location) {
// Code to execute after being located
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
Don't forget to remove the location updates afterwards.
Upvotes: 1