title: Why does the latitude and longitude showing 0 value inside my application?
body: <p>I'm creating a GPS tracker application in android. 
  Now I've written some basic code that pinpoints users location(latitude + longitude) on his/her android screen.</p>
  
  <p>The problem is, when everything is said and done, it's showing a value 0.0. for both latitude and longitude.</p>
  
  <p>Why is it happening?</p>
  
  <p>Below is the code...</p>
  
  <p>MainActivity.java file</p>
  
  <pre><code>package vidyabusapplication.android.com.vidyabusapplication;
  
  import android.Manifest;
  import android.support.v4.app.ActivityCompat;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.test.mock.MockPackageManager;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;  
  import android.widget.Toast;
  
  public class MainActivity extends AppCompatActivity {
  
      Button Btn_trackLocation;
  
      private static final int REQUEST_CODE_PERMISSION = 2;
      String mPermission = Manifest.permission.ACCESS_FINE_LOCATION;
  
      GPSTracker gps;
      TextView location;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          init();
  
          try {
  
              if(ActivityCompat.checkSelfPermission(this, mPermission) != MockPackageManager.PERMISSION_GRANTED) {
              ActivityCompat.requestPermissions(this, new String[] {mPermission}, REQUEST_CODE_PERMISSION);
              }
  
          }catch(Exception ex) {
              ex.printStackTrace();
          }
  
          Btn_trackLocation.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  gps = new GPSTracker(MainActivity.this);
                  if(gps.canYouGetLocation()) {
                      double latitude = gps.getLatitude();
                  double longitude = gps.getLongitude();
                      location.setText(latitude+" "+longitude);
                  }
                  else {
                      gps.enableGpsViaSettingsOptionAlert();
                  }
              }
          });
      }
  
      public void init() {
          location = findViewById(R.id.trackLocationText);
          Btn_trackLocation = findViewById(R.id.startTrackingButton);
      }
  }
  </code></pre>
  
  <p>GPSTracker.java file</p>
  
  <pre><code>Package vidyabusapplication.android.com.vidyabusapplication;
  
  import android.Manifest;
  import android.app.AlertDialog;
  import android.app.Service;
  import android.content.Context;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.location.Location;
  import android.location.LocationListener;
  import android.location.LocationManager;
  import android.os.Bundle;
  import android.os.IBinder;
  import android.provider.Settings;
  import android.support.annotation.Nullable;
  import android.support.v4.app.ActivityCompat;
  
  /**
   * Created by mental on 27/2/18.
   */
  
  public class GPSTracker extends Service implements LocationListener {
  
      private final Context mContext;
  
      boolean isGPSEnabled = false;
      boolean isNetWorkEnabled = false;
      boolean canYouGetLocation = false;
      Location location;
      double latitude, longitude;
      private static final long MIN_DISTANCE_FOR_UPDATES = 10;
      private static final long MIN_TIME_BW_UPDATES = 60000;
  
      protected LocationManager locationManager;
  
      public GPSTracker(Context context) {
          this.mContext = context;
          getLocation();
      }
  
      public Location getLocation() {
          try {
  
              locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
  
              isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
              isNetWorkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  
              if(!isGPSEnabled &amp;&amp; !isNetWorkEnabled) {
  
              }
              else {
                  this.canYouGetLocation = true;
  
                  if (isNetWorkEnabled) {
                      if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                          &amp;&amp; ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                          != PackageManager.PERMISSION_GRANTED) {
                          return null;
                       }
  
                          locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_FOR_UPDATES, this);
  
                      if (locationManager != null) {
                          location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  
                          if (location != null) {
                              latitude = location.getLatitude();
                              longitude = location.getLongitude();
                          }
                      }
                  }
  
                      if (isGPSEnabled) {
  
                          if (location == null) {
                              locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_FOR_UPDATES, this);
                          }
                          if (locationManager != null) {
                              location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  
                              if (location != null) {
                                  latitude = location.getLatitude();
                                  longitude = location.getLongitude();
                              }
                          }
                      }
  
                  }
  
          }catch(Exception ex) {
              ex.printStackTrace();
          }
  
          return location;
      }
  
      public void stopUsingGPS() {
          if(locationManager != null) {
              if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                  &amp;&amp; ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                  != PackageManager.PERMISSION_GRANTED) {
  
                  return;
              }
  
              locationManager.removeUpdates(GPSTracker.this);
          }
      }
  
      public double getLatitude() {
          if(location != null) {
              latitude = location.getLatitude();
          }
  
          return latitude;
      }
  
      public double getLongitude() {
          if(location != null) {
              longitude = location.getLongitude();
          }
  
          return longitude;
      }
  
      public boolean canYouGetLocation() {
          return this.canYouGetLocation;
      }
  
      public void enableGpsViaSettingsOptionAlert() {
  
          AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
  
          dialog.setTitle("GPS DISABLED!");
          dialog.setMessage("Want to enable it now?");
  
          dialog.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  mContext.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
              }
          });
  
          dialog.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  dialog.cancel();
              }
          });
  
          dialog.show();
      }
  
      @Nullable
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      @Override
      public void onLocationChanged(Location location) {
  
      }
  
      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
  
      }
  
      @Override
      public void onProviderEnabled(String provider) {
  
      }
  
      @Override
      public void onProviderDisabled(String provider) {
  
      }
  
  }
  </code></pre>
  
  <p>activity_main.xml file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="vidyabusapplication.android.com.vidyabusapplication.MainActivity"&gt;
  
      &lt;LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          android:gravity="center"&gt;
  
          &lt;TextView
              android:id="@+id/trackLocationText"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Track Location"
              android:textSize="22sp"
              android:textColor="#000000" /&gt;
  
          &lt;Button
              android:layout_width="300dp"
              android:layout_height="60dp"
              android:id="@+id/startTrackingButton"
              android:layout_marginTop="10dp"
              android:text="Start"
              android:textStyle="bold"/&gt;
  
      &lt;/LinearLayout&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>It is to be taken in account that I've all the permissions for location pinpointing inside my androidManifest.xml file.</p>