1

Hello all i know this question is too old and there are many of them but none of the solution is working for me. I am fetching latitude and longitude programmaticaly i tried below code but what is happening is for GPS every time i am getting LastKnownLocation = null also my GPS is enabled on my device, i tried this code on different devices where on some of them i am able to get latitude and longitude from GPS but for most of them it is showing null. I don't know why this code is failing for most of the cases, if any one of you know anything about this or any better way of doing it then please tell me i have spent almost a week digging what's wrong in the above code but nothing help me out.

UPDATE- Is google's FusedLocationApi free or there is limited requests per day ??

public class GPSTracker extends Service implements LocationListener {
private final Context context;

boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;

Location location;

double latitude;
double longitude;

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;

protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.context = context;
    getLocation();
}

public Location getLocation() {
    try {
        locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if(!isGPSEnabled && !isNetworkEnabled) {

        } else {
            this.canGetLocation = true;
            if (isGPSEnabled) {
                Log.d("gps", "gpsenabled");
                if (location == null) {
                    Log.d("gps", "location is null");
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    if (locationManager != null) {
                        Log.d("gps", "locationManager is not null");
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            Log.d("gps", "location is not null");
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                            Log.d("gps", "lat: " + latitude + ", lon: " + longitude);
                        }
                    }
                }
            }
            if (isNetworkEnabled) {
                Log.d("gps", "networkenabled");
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if (locationManager != null) {
                    Log.d("gps", "locationManager is not null");
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        Log.d("gps", "location is not null");
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                        Log.d("gps", "lat: " + latitude + ", lon: " + longitude);
                    }
                }
            }
            //
            LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            Criteria criteria = new Criteria();
            String bestProvider = lm.getBestProvider(criteria, false);
            Log.d("gps", "bestProvider: " + bestProvider);
            Location location = lm.getLastKnownLocation(bestProvider);
            Log.d("gps", "lat: " + location.getLatitude() + ", lon: " + location.getLongitude());
            //
        }
    } catch (Exception e) {

    }
    return location;
}

public void stopUsingGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(GPSTracker.this);
    }
}

public double getLatitude() {
    if (location != null) {
        latitude = location.getLatitude();
    }
    return latitude;
}

public double getLongitude() {
    if (location != null) {
        latitude = location.getLongitude();
    }
    return longitude;
}

public boolean canGetLocation() {
    return this.canGetLocation;
}

public void showSettingsAlert() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
    alertDialog.setTitle("GPS is settings");
    alertDialog.setMessage("GPS is not enabled. Do you want to go to setttings menu ?");
    alertDialog.setPositiveButton("Setting", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            context.startActivity(intent);
        }
    });
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    alertDialog.show();
}

@Override
public void onLocationChanged(Location location) {
    this.location = location;
    latitude = getLatitude();
    longitude = getLongitude();

    Log.d("gps", "onLocationChanged");
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public IBinder onBind(Intent intent) {
    return null;
}
}
Prakash Kumar
  • 829
  • 1
  • 15
  • 32
  • please checkout i have answered here http://stackoverflow.com/questions/39314901/getting-latitude-and-longitude-in-30-seconds – Saveen Sep 26 '16 at 09:33

0 Answers0