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

Implementing LocationProvider class into Stormy

Hi,

I've done Ben Jakuben 's Stormy course and I've also read through the blog post about using Google Play Location Services.

I'm trying to merge the two such that the Stormy app can use the actual location.

I've added the LocationProvider class to the project but I'm not sure that will do it. There's the whole API Key piece that's missing - do I need to do that for each application, or devide/emulator?

Then, I can't see how to get into the LocationProvider class - it is all pretty self-contained. All I want to get out of it is the Lat & Long which I can see used in there, but I just need to understand how to penetrate the class instance and get the class methods working within the Stormy ones.

Is there a simple way to explain this or to demonstrate it?

Thanks,

Steve.

3 Answers

Hi Steve,

This was a cool project to do, after I was finished with the course I was like, hey this only displays the weather for Old Town Alexandria, what if I was on Treehouse Island, wouldn't I want to know what the weather was going to be like. So here is what I found out with a little R&D, with our android phones we have LocationManager that gives us a ton of information, including the lat & long, drop this code below into your project, ideally in the portion where you are setting up the lat & long, pass them into your API and you auto-majically have it pulling back information about the weather pertaining to your location.

If you get stuck, feel free to reach out to me.

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); double longitude = location.getLongitude(); double latitude = location.getLatitude();

Thanks for that Jeffrey - that all looks useful. I shall certainly have a play around with it. At the moment, I'm getting NULL Pointer exceptions as location is holding nothing.

The blog post went through using Google Play Location Services which seem very accurate and also right up to date. They were also asynchronous so any slight delay won't lead to a null pointer exception.

It would be a great addition to the Stormy app if only we could have a couple of pointers as to how to implement one into the other.

The blog post is here, in case you fancy having a look at it. The location-finding app it builds works really well.

http://blog.teamtreehouse.com/beginners-guide-location-android

This will probably answer my question ... a bit of reading for later!

http://developer.android.com/training/location/retrieve-current.html

Sorry Steve, that is the code I emailed myself when I got it working so if my future self should every need to fallback on it. Now that I think of it, I did get a permission error and had to add something in the .manifest file.

When you debug, what errors do you come across.

I added the manifest FINE_LOCATION permission.

The error is a NullPointer exception. The variable, location, isn't holding data quickly enough. The collection of the information is on the main thread. It needs to be async, at which point I might as well use the newer classes.

Sounds like you got a good handle on this.