title: The longitude and latitude always return zero using gps android
body: <p>I am using the following code to get my current location. But the problem I am facing is, it always returns 0.0 for latitude and longitude in the case of Gps. The network provider is working correctly. . I have turned on the GPS settings in the phone and set all the permissions. But still I am facing this.</p>
  
  <p>*<strong>/<em>Java code</em>/*</strong></p>
  
  <pre><code>public class GpsLocationTracker extends Service implements LocationListener
  {
  
  
  private Context mContext;
  private boolean isGpsEnabled = false;
  private boolean isNetworkEnabled = false;
  private boolean canGetLocation = false;
  private Location mLocation;
  private double mLatitude;
  private double mLongitude;
  private static final long MIN_DISTANCE_CHANGE_FOR_UPDATE = 10;
  private static final long MIN_TIME_FOR_UPDATE = 60000;
  private LocationManager mLocationManager;
  
  /**
   * mContext constructor of the class
   */
  public GpsLocationTracker(Context mContext)
  {
  
      this.mContext = mContext;
      getLocation();
  }
  
  
  /**
   * method to finding the location
   */
  public Location getLocation() 
  {
  
      try {
  
          mLocationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
  
          /*getting status of the gps*/
          isGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  
          /*getting status of network provider*/
          isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  
          if (!isGpsEnabled &amp;&amp; !isNetworkEnabled)
          {
              /*no location provider enabled*/
              System.out.println("Gps and Network not enabled");
          } 
          else 
          {
  
              this.canGetLocation = true;
  
              /*getting location from network provider*/
              /* if (isNetworkEnabled) {
  
                  mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_FOR_UPDATE, MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
  
                  if (mLocationManager != null) {
  
                      mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  
                      if (mLocation != null) {
  
                          mLatitude = mLocation.getLatitude();
  
                          mLongitude = mLocation.getLongitude();
                      }
                  }
               }*/
              /*if gps is enabled then get location using gps*/
              if (isGpsEnabled) 
              {
                  if (mLocation == null) 
                  {
                      mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_FOR_UPDATE, MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
  
                      if (mLocationManager != null) 
                      {
                          mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                          System.out.println("location gps"+mLocation);
                          if (mLocation != null) 
                          {
                              mLatitude = mLocation.getLatitude();
  
                              mLongitude = mLocation.getLongitude();
                               System.out.println("Latitude gps"+mLatitude);
                                  System.out.println("longitude gps"+mLongitude);
                          }
  
                      }
                  }
  
                  }
            // }
       }
  
      } catch (Exception e) {
  
          e.printStackTrace();
      }
  
      return mLocation;
  }
  
  /**
   * call this function to stop using gps in our application
   */
  public void stopUsingGps() 
  {
  
      if (mLocationManager != null) 
      {
  
          mLocationManager.removeUpdates(GpsLocationTracker.this);
  
      }
  }
  
  /**
   * @return latitude &lt;p/&gt; function to get latitude
   */
  public double getLatitude()
  {
  
      if (mLocation != null) 
      {
  
          mLatitude = mLocation.getLatitude();
      }
      return mLatitude;
  }
  
  /**
   * @return longitude function to get longitude
   */
  public double getLongitude()
  {
  
      if (mLocation != null) 
      {
  
          mLongitude = mLocation.getLongitude();
  
      }
  
      return mLongitude;
  }
  
  /**
   * @return to check gps or wifi is enabled or not
   */
  public boolean canGetLocation() 
  {
  
      return this.canGetLocation;
  }
  
  /**
   * function to prompt user to open settings to enable gps
   */
  public void showSettingsAlert()
  {
  
      AlertDialog.Builder mAlertDialog = new AlertDialog.Builder(new ContextThemeWrapper(mContext, R.style.AppTheme));
  
      mAlertDialog.setTitle("Gps Disabled");
  
      mAlertDialog.setMessage("gps is not enabled . do you want to enable ?");
  
      mAlertDialog.setPositiveButton("settings", new OnClickListener() 
      {
  
          public void onClick(DialogInterface dialog, int which) 
          {
              // TODO Auto-generated method stub
              Intent mIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
              mContext.startActivity(mIntent);
          }
      });
  
      mAlertDialog.setNegativeButton("cancle", new OnClickListener()
      {
  
          public void onClick(DialogInterface dialog, int which)
          {
              // TODO Auto-generated method stub
              dialog.cancel();
  
          }
      });
  
      final AlertDialog mcreateDialog = mAlertDialog.create();
      mcreateDialog.show();
  }
  
  @Override
  public IBinder onBind(Intent arg0) 
  {
      // TODO Auto-generated method stub
      return null;
  }
  
  public void onLocationChanged(Location location) {
      // TODO Auto-generated method stub
  
  }
  
  public void onProviderDisabled(String provider) {
      // TODO Auto-generated method stub
  
  }
  
  public void onProviderEnabled(String provider) {
      // TODO Auto-generated method stub
  
  }
  
  public void onStatusChanged(String provider, int status, Bundle extras) {
      // TODO Auto-generated method stub
  
  }
  </code></pre>