Core Location Help

Discussion in 'Mac Programming' started by Blakeasd, Jul 16, 2011.

  1. Blakeasd macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello,
    I am trying to make a test app to get the latitude of my current location, but I can't get it to work
    Here is my code:
    Code:
    -(void)awakeFromNib{
    
        if ([CLLocationManager locationServicesEnabled] == TRUE) {
            NSLog(@"Location Services are online");
        } else {
            NSLog(@"Location services are disabled");
        }
    
        CLLocationManager *locationManager = [[CLLocationManager alloc] init];
        [locationManager startUpdatingLocation];
        CLLocation *location = [locationManager location];
        NSString *lattitude = [[NSString alloc]initWithFormat:@"%g",location.coordinate.latitude];
        [lattitudeLabel setStringValue:lattitude];
        [locationManager stopUpdatingLocation];
    }
    
    The lattitudeLabel is an IBOutlet. I don't get any errors or warnings, the problem is my lattitueLabel's value is 0 and I am pretty sure my lattitude isn't 0. (I live in the US :))
    I get the NSAlert asking me to turn on location services and I do, but my label's value doesn't change after I click yes.
    Thanks!
     
  2. kainjow, Jul 16, 2011
    Last edited: Jul 16, 2011

    kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    You probably need to use the delegate and implement locationManager:didUpdateToLocation:fromLocation:

    Edit: like this
    Code:
    - ([color=#aa0d91]void[/color])awakeFromNib
    {
        [color=#3f6e74]locationManager[/color] = [[[color=#5c2699]CLLocationManager[/color] [color=#2e0d6e]alloc[/color]] [color=#2e0d6e]init[/color]];
        [color=#3f6e74]locationManager[/color].[color=#5c2699]delegate[/color] = [color=#aa0d91]self[/color];
        [[color=#3f6e74]locationManager[/color] [color=#2e0d6e]startUpdatingLocation[/color]];
    }
    
    - ([color=#aa0d91]void[/color])locationManager:([color=#5c2699]CLLocationManager[/color] *)manager didUpdateToLocation:([color=#5c2699]CLLocation[/color] *)newLocation fromLocation:([color=#5c2699]CLLocation[/color] *)oldLocation
    {
        [[color=#3f6e74]lattitudeLabel[/color] [color=#2e0d6e]setStringValue[/color]:[[color=#5c2699]NSString[/color] [color=#2e0d6e]stringWithFormat[/color]:[color=#c41a16]@"%g"[/color], newLocation.[color=#5c2699]coordinate[/color].latitude]];
        [[color=#3f6e74]locationManager[/color] [color=#2e0d6e]stopUpdatingLocation[/color]];
    }
    
     
  3. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009

Share This Page