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 Review: Hooking Up the Model to the View

Sandro Meschiari
Sandro Meschiari
18,418 Points

how to add location service to weather app

i would like to add the location service to my weather app as suggested on the extra challenge from Ben Jakuben but i got lost half way can anybody give me some tips?

2 Answers

This is an easy way to accomplish this. Adapt it to your Fragment or Activity, add the manifest permission and don't forget to update the location of your emulator (in case you are using one).

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
public class MyFragment extends Fragment implements
        ConnectionCallbacks, OnConnectionFailedListener, LocationListener {

    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;

    public MyFragment() {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = //...

        // your stuff


        return view;

    public void onStart() {

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())

    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();

    protected void startLocationUpdates() {
        Log.i(TAG, "Started listening for location updates...");
                mGoogleApiClient, mLocationRequest, this);

    protected void stopLocationUpdates() {
        Log.i(TAG, "Stopped listening for location updates");
                mGoogleApiClient, this);

    public void onConnected(Bundle connectionHint) {
        Log.i(TAG, "Connected");

    public void onConnectionFailed(ConnectionResult result) {
        Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());

    public void onConnectionSuspended(int cause) {
        Log.i(TAG, "Connection suspended");

    public void onLocationChanged(Location location) {
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();


        // do whatever you want to do with lat and long

I didn't get how to make this work out.

Can you please share project included with location code.