Reputation: 165
Hello I am using following code to get location usin GPS I have given all required permission in AndroidManifest.xml
if (DeviceUtilities.isNetworkAvailable(context))
{
Toast.makeText(HomeScreen.this, "Getting location from GPS",Toast.LENGTH_SHORT).show();
LocationManager lm;
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_FINE);
String bestProvider = lm.getBestProvider(c, true);
Location location = lm.getLastKnownLocation(bestProvider);
if(location!=null)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
But getting null values for latitude and longitude. How do I can get location? Please suggest. Thanks in advance.
Upvotes: 0
Views: 251
Reputation: 2696
1) create the location listener class TrackLocation
public class TrackLocation extends Service implements LocationListener {
private final Context mContext;
// we can get location from gps or network
// Flag for GPS status
boolean isGPSEnabled = false;
// Flag for network status
boolean isNetworkEnabled = false;
// Flag for GPS status
boolean canGetLocation = false;
Location location; // Location
double latitude; // Latitude
double longitude; // Longitude
// distance to change Updates in meters
private static final long MIN_DISTANCE = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME = 1000 * 60 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public TrackLocation(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// Getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// Getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// No network provider is enabled
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME,
MIN_DISTANCE, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// If GPS enabled, get latitude/longitude using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME,
MIN_DISTANCE, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app.
* */
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/Wi-Fi enabled
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
2) then in main class do this
TrackLocation gps = new TrackLocation(this);
//Check if GPS enabled
if(gps.canGetLocation()) {
// now get get coordinates
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
} else {
// Can't get location.
}
Upvotes: 1
Reputation: 448
"getLastKnownLocation" can return null if the phone doesn't have a last location, which means that other apps have not requested or obtained the location recently.
If you get null using getLastKnowLocation, you have to get the location asking the gps or the network and waiting in your listener for the response.
Some snippets of code:
LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER OR LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
You have to implement your locationListener to receive the position.
More info here: http://developer.android.com/reference/android/location/LocationManager.html
Note: this way is using LocationManager, you can use also play services but I prefer using the Location Manager.
Upvotes: 0