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

How do I acquire my location for "Stormy" weather app?

I added google play resources to my project and tried adding some code guided by Ben's tutorial. But I'm not getting anywhere. I don't know where exactly in my code should I ask for location. I tried using android developers site but I'm not yet able to discern which part of the info is important for my project, and I just end up with tons of code and confused.

I would appreciate if someone would help me.

Cheers

9 Answers

The Google docs are also pretty useful.

http://developer.android.com/training/location/index.html

Although the tutorial Ken Alger just posted looks spot on!

Steve.

Ken Alger
Ken Alger
Treehouse Teacher

Steve;

I worked through the android "tutorial" a few times and found it to be less helpful that some of the other information available. I looked around quite a bit at the time I was working on that project and the tutorial I posted was but one of several. I think it does a rather good job of explaining what needs to occur.

Happy coding,

Ken

I found this code which helps.

//Injected views above


LocationManager locationManager;

String provider;
Location location;

private double mLatitude;
private double mLongitude;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.inject(this);


    locationManager =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    Criteria c=new Criteria();
    //if we pass false than
    //it will check first satellite location than Internet and than Sim Network
    provider=locationManager.getBestProvider(c, false);
    location=locationManager.getLastKnownLocation(provider);
    if(location!=null)
    {
        DecimalFormat df = new DecimalFormat("##.####");
        double unformatedLatitude = location.getLatitude();
        double unformatmedLongitude =location.getLongitude();

        mLatitude = Double.valueOf(df.format(unformatedLatitude));
        mLongitude = Double.valueOf(df.format(unformatmedLongitude));

        Toast.makeText(this, "LOCATION SUCCESS!!!", Toast.LENGTH_LONG);
    }
    else
    {
        mLatitude = 40.7744;
        mLongitude = -73.9042;
        Toast.makeText(this, "ASTORIA DEFAULT", Toast.LENGTH_LONG).show();
    }




    mProgressBar.setVisibility(View.INVISIBLE);

[[[[ then call the getForecast methods ]]]]]

I've been lookinginto this a bit and must say that it is not very easy!

I have so far got as far as being able to bring back my lat & long which I can pass to forecast.io in the URL.

If that would help you, I'll see if I can set up a Git repo and you can see my code. I'll be working more on it to try to bring back a town/city name next.

First, though, I'm updating it to show the next 5 days forecasts in swipable screens.

Happy to share the project! We can both work on it.

Steve.

Both projects are now on GitHub and ready to be forked.

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Saicko;

In a nutshell you need to utilize the device's GPS and like Steve Hunter mentioned, pass that data to your weather API. That can be done using the location service provided by Android. There are many tutorials available on this process, one of which can be found here.

From a User Experience aspect, you will probably want to convert the Lat & Long data into a specific city as well. This can be a bit more cumbersome, but after working with several different APIs, I found Google's implementation to be rather straight forward and easy to implement.

Like Steve said, post back with any additional questions. I'm sure folks here on the forum will be pleased to assist.

Happy coding,

Ken

Thanks for quick response!

I'm looking into tutorials you recommended. Will come back to you as soon as i figure something out. Also I will look into getting location name. Will share when/if I get it working.

Cheers and thanks again

also don't forget to implement LocationListener

The only thing I can't get now is updating the location name, since it's not Alcatraz.... seems like there is no JSON data in forecast.io that returns the location/city name based on the coordinates

I finally figured out how it's done after reading all the material you guys recommended. Basically you have to build googleApiclient and connect it. And in onConnected method put the code to get coordinates from last location.

I've read something about reverse geocoding to get location name, but I couldn't be bothered to tweak it, seemed easier to change weather API. Now I use openweathermap.org, it returns location name.

Good luck

Greg Andrews
Greg Andrews
7,554 Points

I used the refractored location example that Ben wrote and then added the code from Stormy to it.