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

Juan Alvarado
Juan Alvarado
3,740 Points

Having on demand location updating.

So I've gone ahead and completed this app. It came out great and also went ahead and added location awareness to it using this guide http://blog.teamtreehouse.com/beginners-guide-location-android

It works for the most part except that I'm seeing an issue that might become problematic as I go to the next lesson with the ListViews and RecyclerViews.

When I added the location awareness stuff, I essentially had to move the code to get a forecast based on lat and lng to the onConnected method of the GoogleApiClient interface.

Like this:

@Override
    public void onConnected(Bundle bundle) {
         Location location =     LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if(location == null) {
                LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }
        else {
            handleNewLocation(location);
        }
    }

    private void handleNewLocation(Location location) {
        mLatitude = location.getLatitude();
        mLongitude = location.getLongitude();
        getForecast(mLatitude, mLongitude);

        mRefreshImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getForecast(mLatitude, mLongitude);
            }
        });

    }

As you can see getForecast() gets called only from onConnected's handleNewLocation helper method. If I were to take the calls out of there, mLatitude and mLongitude could come back empty and I'd be showing weather for (0,0).

Any suggestions on how this is usually approached?

2 Answers

I set default values for when location wasn't available.

Feel free to browse my project here - I haven't progressed it for a while but must pick it up again!

The location service does work, so you can get up-to-date weather at your current location. I just want to enable a description of location so that I can amend the label to show the nearest town/city.

Hope that helps!

Steve.

Juan Alvarado
Juan Alvarado
3,740 Points

It looks like we have a similar scenario. I don't set a default location ( I should), and I depend on the LocationServices to run my flow. Here my code.

I'm still a bit fuzzy on the understanding of the LocationServices. Right now to me it seems as though we have to wait for the GoogleApiClient to call the onConnected method so we can assure we have a location, and the requestLocationUpdates keeps running on the background for any application to use the getLastLocation() method.

But this still depends on the onConnected() method. What if I wanted to add a SwipeRefreshLayout? This interface requires that we implement the SwipeRefreshLayout.OnRefreshListener(), so at some point, if the getLastLocation() contains null, how would we go about having a way to ask for an update on the location inside the OnRefreshListener method?

This is where I'm not entirely sure how the structure would work.

I think you're a way ahead of me on this - I hadn't thought about that scenario.

Thanks for the Github link; I've followed you on there.

I was thinking to have a last known location stored to cater for scenarios where location services return null. I've not looked at the swipe refresh thing - that's an interesting one. But, surely, the service will cater for this requirement. I guess I need to figure out what other events are triggered so they can be utilised to get the information required.