title: Android - Why is my Current Location app always return 0 Latitude and 0 Longitude?
body: <p>The return value whenever i call getCurrentLocation() function is always Latitude: 0.0 and Longitude: 0.0</p>
  
  <p><a href="http://i.stack.imgur.com/Fxu5v.png" rel="nofollow">The Output. The result is also the same as both on the emulator and on the phone.</a></p>
  
  <p>I used an actual phone and the emulator, both of them still give 0 and 0.</p>
  
  <p>These are in my AndroidManifest.xml</p>
  
  
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.thesis.adrianangub.myapplication" &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;
  
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme" &gt;
  
  
          &lt;!--
               The API key for Google Maps-based APIs is defined as a string resource.
               (See the file "res/values/google_maps_api.xml").
               Note that the API key is linked to the encryption key used to sign the APK.
               You need a different API key for each encryption key, including the release key that is used to
               sign the APK for publishing.
               You can define the keys for the debug and release targets in src/debug/ and src/release/. 
          --&gt;
          &lt;meta-data
              android:name="com.google.android.geo.API_KEY"
              android:value="@string/google_maps_key" /&gt;
  
          &lt;activity
              android:name=".MapsActivity"
              android:label="@string/title_activity_maps" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>These are in my MapsActivity.java</p>
  
  <pre><code> package com.thesis.adrianangub.myapplication;
   public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, GoogleMap.OnMarkerDragListener, GoogleMap.OnMapLongClickListener, View.OnClickListener
   {
  
  //Our Map
  private GoogleMap mMap;
  
  //To store longitude and latitude from map
  private double longitude;
  private double latitude;
  
  //Buttons
  private Button buttonSave;
  private Button buttonCurrent;
  private Button buttonView;
  
  //Google ApiClient
  private GoogleApiClient googleApiClient;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_maps);
      // Obtain the SupportMapFragment and get notified when the map is ready to be used.
      SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
              .findFragmentById(R.id.map);
      mapFragment.getMapAsync(this);
  
      //Initializing googleapi client
      googleApiClient = new GoogleApiClient.Builder(this)
              .addConnectionCallbacks(this)
              .addOnConnectionFailedListener(this)
              .addApi(LocationServices.API)
              .build();
  
      //Initializing views and adding onclick listeners
      buttonSave = (Button) findViewById(R.id.buttonSave);
      buttonCurrent = (Button) findViewById(R.id.buttonCurrent);
      buttonView = (Button) findViewById(R.id.buttonView);
      buttonSave.setOnClickListener(this);
      buttonCurrent.setOnClickListener(this);
      buttonView.setOnClickListener(this);
  }
  
  @Override
  protected void onStart() {
      googleApiClient.connect();
      super.onStart();
  }
  
  @Override
  protected void onStop() {
      googleApiClient.disconnect();
      super.onStop();
  }
  
  //Getting current location
  private void getCurrentLocation() {
      mMap.clear();
      //Creating a location object
      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) {
          // TODO: Consider calling
          //    ActivityCompat#requestPermissions
          // here to request the missing permissions, and then overriding
          //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
          //                                          int[] grantResults)
          // to handle the case where the user grants the permission. See the documentation
          // for ActivityCompat#requestPermissions for more details.
          return;
      }
      Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
      if (location != null) {
          //Getting longitude and latitude
          longitude = location.getLongitude();
          latitude = location.getLatitude();
  
          //moving the map to location
          moveMap();
      }
  }
  
  //Function to move the map
  private void moveMap() {
      //String to display current latitude and longitude
      String msg = latitude + ", "+longitude;
  
      //Creating a LatLng Object to store Coordinates
      LatLng latLng = new LatLng(latitude, longitude);
  
      //Adding marker to map
      mMap.addMarker(new MarkerOptions()
              .position(latLng) //setting position
              .draggable(true) //Making the marker draggable
              .title("Current Location")); //Adding a title
  
      //Moving the camera
      mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
  
      //Animating the camera
      mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
  
      //Displaying current coordinates in toast
      Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
  }
  
  @Override
  public void onMapReady(GoogleMap googleMap) {
      mMap = googleMap;
      LatLng latLng = new LatLng(-34, 151);
      mMap.addMarker(new MarkerOptions().position(latLng).draggable(true));
      mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
      mMap.setOnMarkerDragListener(this);
      mMap.setOnMapLongClickListener(this);
  }
  
  @Override
  public void onConnected(Bundle bundle) {
      getCurrentLocation();
  }
  
  @Override
  public void onConnectionSuspended(int i) {
  
  }
  
  @Override
  public void onConnectionFailed(ConnectionResult connectionResult) {
  
  }
  
  @Override
  public void onMapLongClick(LatLng latLng) {
      //Clearing all the markers
      mMap.clear();
  
      //Adding a new marker to the current pressed position
      mMap.addMarker(new MarkerOptions()
              .position(latLng)
              .draggable(true));
  }
  
  @Override
  public void onMarkerDragStart(Marker marker) {
  
  }
  
  @Override
  public void onMarkerDrag(Marker marker) {
  
  }
  
  @Override
  public void onMarkerDragEnd(Marker marker) {
      //Getting the coordinates
      latitude = marker.getPosition().latitude;
      longitude = marker.getPosition().longitude;
  
      //Moving the map
      moveMap();
  }
  
  @Override
  public void onClick(View v) {
      if(v == buttonCurrent){
          getCurrentLocation();
          moveMap();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And this is my activity_maps.xml</p>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MapsActivity"&gt;
  
  &lt;fragment xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:map="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/map"
      android:name="com.google.android.gms.maps.SupportMapFragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.thesis.adrianangub.myapplication.MapsActivity" /&gt;
  
  &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="bottom"
      android:orientation="vertical"&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="#cc3b60a7"
          android:orientation="horizontal"&gt;
  
          &lt;Button
              android:text="Curr"
              android:id="@+id/buttonCurrent"
              android:onClick="getCurrentLocation"
              android:layout_width="50dp"
              android:layout_height="50dp"
              android:layout_margin="15dp" /&gt;
  
          &lt;Button
              android:text="Save"
              android:id="@+id/buttonSave"
              android:layout_width="50dp"
              android:layout_height="50dp"
              android:layout_margin="15dp" /&gt;
  
  
          &lt;Button
              android:text="View"
              android:id="@+id/buttonView"
              android:layout_width="50dp"
              android:layout_height="50dp"
              android:layout_margin="15dp"/&gt;
  
      &lt;/LinearLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>