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

Adding Current Location button to Stormy Alternative Method

After reading Ben Jakuben's post about location services, I am trying to implement a Current Location button to Stormy. I have managed to get it to work by, under my Button's OnClickListener, telling the app to connect to the GoogleApiClient and then processing the current location. However, to get the button to refresh the location, I had to tell the app to disconnect from the GoogleApiClient so that the button may reconnect the app and "refresh" the location.

This current method seems rather inefficient, especially since I just want to refresh the location and not have to disconnect and reconnect to the GoogleApi to do this. Are there any alternative methods to refreshing the location on the app without disconnecting and reconnecting from the API?

2 Answers

I haven't read through it but this sounds like it might be useful.

http://www.androidhive.info/2015/02/android-location-api-using-google-play-services/

Yup, this post had exactly what I was looking for. Thanks Ozhan!

To everyone that's interested, to implement a Current Location button, you just need to place the LocationServices command under the Button Click listener. This way, the GoogleApi does not have to be continuously reconnected and disconnected. There's still a minor problem when starting the app with GPS disabled, but I think that can be fixed using a few if loops.

It sounds like your app is searching for location when you start it and by pressing the button it is sending a second request which gets queued until the first request is completed. The most efficient thing would be to not have the button at all and let the app find its location in the background. The flow is you turn on the app, it finds your location and gives you the forecast for where you are, you exit the app. It's unlikely you'll travel far enough to need a refresh button.

Yes, that's correct. In my OnCreate method, the new GoogleApiClient is created and then connected.

However, what should I do if I really wanted the Current Locations button? I thought about moving the GoogleApiClient creation to the OnClick listener, but then a new connector would be created everytime I clicked the button. I'm not too sure if this is efficient either.

Also, just to give some context, I currently live in between cities. I have three buttons: two of them are for the two cities next to me and the third is the Current Location button I am trying to create. That's why I would really like to have the Current Locations button.