Getting Current Location in City Name

Discussion in 'iOS Programming' started by Wellington2k, Dec 13, 2011.

  1. Wellington2k macrumors regular

    Joined:
    Jun 4, 2011
    #1
    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!
     
  2. Wellington2k thread starter macrumors regular

    Joined:
    Jun 4, 2011
    #2
    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.
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    Your second post was less than half an hour after the first! Hardly seems like hours! :confused:

    I keed, I keed. :D

    Good job on finding a solution! :)
     
  4. sridvijay macrumors newbie

    Joined:
    Mar 9, 2010
    #4
    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!
     

Share This Page