Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Wellington2k

macrumors regular
Original poster
Jun 4, 2011
131
0
Hello.

I'm trying to get weather for an app of mine.
I successfully got the weather from Google, but now I want it to get
it from current location. (Instead of typing the Zip Code)

So I've tried to get my current location and I've been somewhat successful.
But I can't seem to get the city's name.

I've tried this tutorial: http://stackoverflow.com/questions/1382900/how-to-retrive-users-current-city-name
But sadly MKReverseGeocoder is not supported by iOS 5.
I tried replacing with CLLocation, but initWithCoordinates can't be used with it.

So what I'm asking is for help with getting MKReverseGeocoder to work by replacing it and (if possible) help with getting my current location.

Thanks!
 
After hours and HOURS of testing and debugging I finally got it!

Code:
NSString *test;
CLLocationManager *locationManager;
CLPlacemark * myPlacemark;

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [locationManager stopUpdatingLocation];
    
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        for (CLPlacemark * placemark in placemarks) {
            test = [placemark locality];
            NSLog(@"%@", test);
        }    
    }];
}


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{
    NSLog(@"locationManager:%@ didFailWithError:%@", manager, error);
}

- (void)reverseGeocoder:(CLGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSLog(@"reverseGeocoder:%@ didFailWithError:%@", geocoder, error);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    [locationManager startUpdatingLocation];
}

"test" string outputs the city.
 
Lifesaver!

Wellington,

I can't thank you enough for posting that code! I FINALLY was able to understand it and get my application to start going! Thank you, thank you, thank you!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.