Not able to get Location details using GoogleClientAPI in android

82 views Asked by At

I tried getting the latitude and longitude of the users location, using GoogleClientAPI. I am not seeing any errors , but I am not able to log the location details.

I am new to android started just a couple of week back.

I have attached my code below:

public class MainActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener{



@InjectView(R.id.idTemparatureValue) TextView mTemparatureValue;
@InjectView(R.id.idHumidityValue) TextView mHumidityValue;
@InjectView(R.id.idRainValue) TextView mRainValue;
@InjectView(R.id.idSummaryValue) TextView mSummaryValue;
@InjectView(R.id.idTimeValue) TextView mTimeValue;



private final String TAG=MainActivity.class.getSimpleName();
GoogleApiClient mGoogleApiClient;
private String apiKey="802**********a1fdf5";
private double latitude;/* =13.0827;*/
private double longitude ;/*=80.2707;*/
Forecast mForecast = new Forecast();


private String url;/*="https://api.forecast.io/forecast/"+apiKey+"/"+latitude+","+longitude;*/


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mGoogleApiClient=new GoogleApiClient.Builder(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
    mGoogleApiClient.connect();


    ButterKnife.inject(this);


  .......
  protected void onResume(){
    super.onResume();
    mGoogleApiClient.connect();
}

protected void onPause(){
    super.onPause();
    if(mGoogleApiClient.isConnected()){
        mGoogleApiClient.disconnect();
    }
}





@Override
public void onConnected(Bundle bundle) {

    Location location=LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (location!=null){
        handleLocation(location);
    }


}

private void handleLocation(Location location) {
    Log.i(TAG,location.toString());
    Log.i(TAG,"Latitude : "+location.getLatitude());
    Log.i(TAG,"Longitude : "+location.getLongitude());
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}
}
0

There are 0 answers