title: Getting Latitude and Longitude 0.0 in Android M
body: <p><strong>I am suffering from the problem of getting value 0.0 as latitude and longitude in Android Marshmallow (API 23) i.e. 6.0.</strong></p>
  
  <p>I have searched a lot regarding this but didn't find solution of it. My code is fully working for the other API versions which are less than 23.</p>
  
  <p>I have also put the code for selfPermission and which all are working instead it's giving me 0.0 as late and long.</p>
  
  <p>Please have a look on the way I am following:</p>
  
  <p>GPSTracker.java</p>
  
  <pre><code>public class GPSTracker extends Service implements LocationListener {
  
      private  Context mContext;
  
      // 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
  
      // The minimum distance to change Updates in meters
      private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1000; 
  
      // 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;
  
      Activity activity;
  
      public GPSTracker() {
      }
  
      public GPSTracker(Context context, Activity activity) {
          this.mContext = context;
          this.activity = activity;
          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 &amp;&amp; !isNetworkEnabled) {
                  // No network provider is enabled
              } else {
                  this.canGetLocation = true;
                  if (isNetworkEnabled) {
                      int requestPermissionsCode = 50;
                      if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                          ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, requestPermissionsCode);
  
                      } else {
                          locationManager.requestLocationUpdates(
                                  LocationManager.NETWORK_PROVIDER,
                                  MIN_TIME_BW_UPDATES,
                                  MIN_DISTANCE_CHANGE_FOR_UPDATES, 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) {
                          if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                              ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 50);
  
                          } else {
                              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();
          }
  
          return location;
      }
  
  
      /**
       * Stop using GPS listener
       * Calling this function will stop using GPS in your app.
       * */
      public void stopUsingGPS() {
          if (locationManager != null) {
              if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                  ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 50);
  
              } else {
                  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;
      }
  
  
      /**
       * Function to show settings alert dialog.
       * On pressing the Settings button it will launch Settings Options.
       * */
      public void showSettingsAlert(){
          AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
  
          // Setting Dialog Title
          alertDialog.setTitle("GPS is settings");
  
          // Setting Dialog Message
          alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
  
          // On pressing the Settings button.
          alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog,int which) {
                  Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                  mContext.startActivity(intent);
              }
          });
  
          // On pressing the cancel button
          alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                  dialog.cancel();
              }
          });
  
          // Showing Alert Message
          alertDialog.show();
      }
  
  
      @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;
      }
   }
  </code></pre>
  
  <p>DashboardActivity.java:</p>
  
  <p>In onCreate() I have initialized GPSTracker:</p>
  
  <pre><code>gps = new GPSTracker(mContext,DashboardActivity.this);
  
          // Check if GPS enabled
          if(gps.canGetLocation()) {
  
              double latitude = gps.getLatitude();
              double longitude = gps.getLongitude();
  
              // \n is for new line
              Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
          } else {
              // Can't get location.
              // GPS or network is not enabled.
              // Ask user to enable GPS/network in settings.
              gps.showSettingsAlert();
          }
  </code></pre>
  
  <p>AndroidManifest.xml</p>
  
  <pre><code>&lt;permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
      &lt;permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  
  
      &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  </code></pre>
  
  <p>I have gone through many links provided in Stack Overflow but none of them helped me to solve this problem.</p>
  
  <p>I would appreciate any kind of help here.</p>