title: Can't Get GPS Current Location's Latitude and Longitude Android
body: <p>I am trying to get Current Location using either <code>GPS</code> or <code>Network Provider</code>. My device's <code>GPS</code> is enabled but I'm not getting <code>Latitude</code> and <code>Longitude</code>.</p>
  
  <p><strong>GpsTracker.java:</strong></p>
  
  <pre><code>public class GpsTracker extends Service implements LocationListener {
  
          private final Context mContext;
          // flag for GPS Status
          boolean isGPSEnabled = false; 
          // flag for network status
          boolean isNetworkEnabled = false;
          boolean canGetLocation = false; 
          Location location;
          public double latitude;
          public double longitude;
  
          // The minimum distance to change updates in metters
          private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 
           // metters
  
           // The minimum time between updates in milliseconds
           private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
  
                      // Declaring a Location Manager
                      protected LocationManager locationManager;
  
                      public GpsTracker(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 Network Provider Enabled get lat/long using GPS Services
                                  if (isNetworkEnabled) {
                                      if (location == null) {
                                          locationManager.requestLocationUpdates(
                                                  LocationManager.NETWORK_PROVIDER,
                                                  MIN_TIME_BW_UPDATES,
                                                  MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  
  
                                          if (locationManager != null) {
                                              location = locationManager
                                                      .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                                              updateGPSCoordinates();
                                          }
                                      }   
                                  }
  
                                   // if GPS Enabled get lat/long using GPS Services
                                  if (isGPSEnabled) {
                                      if (location == null) {
                                          locationManager.requestLocationUpdates(
                                                  LocationManager.GPS_PROVIDER,
                                                  MIN_TIME_BW_UPDATES,
                                                  MIN_DISTANCE_CHANGE_FOR_UPDATES, 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();
                              Log.e("Error : Location",
                                      "Impossible to connect to LocationManager", e); 
                          }
  
                          return location;
                      }
  
                      public void updateGPSCoordinates() {
                          if (location != null) {
                              latitude = location.getLatitude();
                              longitude = location.getLongitude();
                          } 
                      }
  
                      public double getLatitude() {
                          if (location != null) {
                              latitude = location.getLatitude();
                          }
  
                          return latitude;
                      }
  
                      public double getLongitude() { 
                          if (location != null) {
                              longitude = location.getLongitude();
                          }
  
                          return longitude;
                      }
                  }
  </code></pre>
  
  <p>Service.Java</p>
  
  <pre><code>    public class WifiScaningService extends IntentService {
  
          GpsTracker gpsTracker;
          double latitude;
              double longitude;
          public WifiScaningService() {
                  super("WifiScaningService"); 
  
              }
  
              @Override 
              public IBinder onBind(Intent intent) {    
                  // TODO: Return the communication channel to the service.
                  throw new UnsupportedOperationException("Not yet implemented");
              }
  
              @Override
              public void onCreate() {
                  Log.e("service call","service call");
              }  
  
  
              @Override
              protected void onHandleIntent(Intent intent) {
  
              } 
              @Override
  
  
              public int onStartCommand(Intent intent, int flags, int startId) { 
                      Log.i("LocalService", "Received start id " + startId + ": " + intent);
  
                      gpsTracker = new GpsTracker(this); 
                      String stringLatitude = String.valueOf(gpsTracker.latitude);
                      latitude = Double.parseDouble(stringLatitude);
                      Log.e("Latitude",stringLatitude);
                      String stringLongitude = String.valueOf(gpsTracker.longitude);
                      longitude = Double.parseDouble(stringLongitude);
                      Log.e("Longitude",stringLongitude); 
  
                  return START_STICKY;
              }
      }
  </code></pre>
  
  <p>I have included necessary permissions in AndroidManifest.xml file. Required to show current location.</p>
  
  <pre><code>&lt;uses-permission
    android:name="android.permission.ACCESS_COARSE_LOCATION"/&gt;
       &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
          uses-permission android:name="android.permission.INTERNET"/&gt;
  </code></pre>