Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Weather App (2015) Hooking Up the Model to the View Wrapping Up

Ragesh Kanagaraj
Ragesh Kanagaraj
3,637 Points

Not able to Access Location using GoogleApiClient.

I followed the steps in the blog, to retrieve latitude and Longitude and pass it to the URL, so that we can get weather updates based on our location.

I did not get any errors but I was not able to LOG my location values, I tried running the app on my phone instead of emulator.

My code :

''' 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="8022***********5";
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) {

}

}