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
jen0vah
2,636 Pointsupdating textview with geocoder object
It's (still) about the weather app. I figured out how to incorporate the location tracking and updating the Latitude and Longitude works perfectly now as my Toast shows. Now I almost forgot the most important part. The location name is still hard coded!
So I figured I have to reverse engineer the coordinates to get a city name / country name etc. So in the same method where I handle getting the new Lat. & Long I created a new Geocoder object with the help of the Documentation and it seem to work judging from the Toasts i get.
Now my question is how do I get my label updated with the city name and the country code? We updated the other Labels through the @Bind mechanic in the updateDisplay() method as far as I understand that. But this doesnt seem to work now?
In the MainActivity.java
private void handleNewLocation(Location location) throws IOException {
Log.d(TAG, location.toString());
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(currentLatitude, currentLongitude, 1);
if(addresses != null && addresses.size() > 0){
Toast.makeText(this, addresses.get(0).getLocality(),Toast.LENGTH_SHORT).show();
Toast.makeText(this, addresses.get(0).getCountryCode(),Toast.LENGTH_SHORT).show();
}
}
4 Answers
Steve Hunter
57,712 PointsHi Christopher,
What format is your location in? Can you convert it to a string?
Can you set a new member variable to hold that location string then, inside of updateDisplay amend the locationLabel using setText(mLocationString)?
It doesn't need to be in updateDisplay really - as soon as the location is fixed, fire an update. Can you use a location event? Something like onConnected or createLocationRequest?
I should think you've done the hard bit - I never got stuck into getting the location text; I don't know how that's done. But Setting a label to say the right thing shouldn't be a challenge! What do your toasts look like?
Steve.
jen0vah
2,636 PointsOkay tell me if you're having trouble. If yes I can share my whole project
Steve Hunter
57,712 PointsDid you change your name or am I going crazy?!
I'm now trying to get the location label at the bottom of the activity_daily_forecast.xml to update ... Not have much joy and I haven't separated the location stuff into a separate file - I really should have done!!
jen0vah
2,636 Pointshhaha you are right :D i changed my name. I disliked how my first and last name was put together as a username here :p yeah about that. I kinda have the same problem right now here as well. I didn't separate anything either. My project pretty much looks like the tutorial one except the location tracking now.
In the end I'm not sure if i will keep the location label there tho. But making it work over there as well would be nice too
Steve Hunter
57,712 PointsI'll give it some thought ... I need to look when the instance of DailyForecast is created - that determines when the info can be passed across.
Just adding it to getDailyForecast should work. I'll try it ...
jen0vah
2,636 Pointsokay I'll try to fix that here as well. if you found a solution don't hold back and tell me :D!
jen0vah
2,636 Pointsjen0vah
2,636 PointsHi Steve i don't know what i was thinking. Guess sometimes you have to step back and stop thinking about something for a while then the answer will pop up right in front of you...
I did it know and it was just adding 3 lines in the end..
Modified the handleNewLocation method like this:
and then in the updateDisplay method id added the following line:
mLocationLabel.setText(mCity + "," + mCountryCode);The Toasts in the handleNewLocation Method are giving me the Cityname and the Countrycode as a String already. just had to save them in a member variable and use the Textview_id.setText() method to update it in the end. Sorry for asking something like that.. Guess I should have thought about it 2 minutes longer -.-
Greetings~
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsGlad to help! I've just been updating my app with your location detail code to see if I can get it working too.
Steve.
[EDIT] And it works perfectly. Thank you very much indeed. :-))