-1

I just started coding in Objective C, and am making an app that will track the users location, and send an alert with the latlng in it. This code isn't complete in the slightest, but I ran into a problem with trying to use the variable I created "lat" in the "viewDidLoad" for the alert. I declared the variable in the CLLocationManager delegate/method(I don't really know what it's called) and I don't know how to use it in other places.

    - (void)viewDidLoad
{
    locationManager =[[CLLocationManager alloc] init];

locationManager.delegate = self; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = 100.0f;
[locationManager startUpdatingLocation];


UIAlertView *message = [[UIAlertView alloc] initWithTitle: @"Your current Latitude is:"
                                                  message:This is where I want to put the variable "lat"
                                                 delegate:nil
                                        cancelButtonTitle:@"OK"
                                        otherButtonTitles:nil];   
[message show];    

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

}

-(void) locationmanager: (CLLocationManager *) manager
        didUpdateToLocation: (CLLocation *) newLocation
        fromLocation: (CLLocation *) oldLocation
{ 
    NSString *lat = [[NSString alloc] initWithFormat: @"%g",newLocation.coordinate.latitude];
    NSString *lng = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude];  
} 

Any help would be appreciated!

michael03m
  • 161
  • 1
  • 2
  • 10
  • I had just forgot to @synthesize the variables I declared in the .h file. – michael03m Mar 01 '12 at 02:57
  • Yes, you need to use properties to do this. There are some great videos on iTunesU that really helped me out in the start. Check them out here. http://itunes.apple.com/us/itunes-u/ipad-iphone-application-development/id473757255 Cheers. – Jamie Mar 01 '12 at 01:03

1 Answers1

0

Simple solution. Your ViewController .h should look something like this

@interface ViewController : UIViewController {
    NSString *lat;
    NSString *lng;
}

then this becomes

-(void) locationmanager: (CLLocationManager *) manager
    didUpdateToLocation: (CLLocation *) newLocation
    fromLocation: (CLLocation *) oldLocation
{ 
    lat = [[NSString alloc] initWithFormat: @"%g",newLocation.coordinate.latitude];
    lng = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude];  
    NSLog(lat);
    NSLog(lng);
} 

NSlog simple out puts to the console which is locate just below where you code

remove

UIAlertView *message = [[UIAlertView alloc] initWithTitle: @"Your current Latitude is:"
                                              message:This is where I want to put the variable "lat"
                                             delegate:nil
                                    cancelButtonTitle:@"OK"
                                    otherButtonTitles:nil];   
[message show];    

Because that will just show an alert as soon as the view is loaded

Hope this helps

Michael Smith
  • 498
  • 2
  • 5
  • 15
  • So would the NSLog just output to the console whenever it got a newLocation? – michael03m Mar 01 '12 at 02:30
  • @Michael: Your `NSLog` calls are incorrect. The first argument to `NSLog` must be a format string. – jscs Mar 01 '12 at 02:54
  • Actually the NSLog calls are fine. I wasn't using them in there strictest sense. If you want to be 100% correct then use NSLog(@"%@",lat); NSLog(@"%@",lng); – Michael Smith Mar 01 '12 at 11:46
  • Yes the was NSLog's should output to the console everytime your location is updated, That is if I am remembering what "-(void) locationmanager: (CLLocationManager *) manager didUpdateToLocation: (CLLocation *) newLocation fromLocation: (CLLocation *) oldLocation" does – Michael Smith Mar 01 '12 at 11:48