1

Im trying to get the lat and long values generated in a void function and use them within another function. Any help grateful.

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
 float latDeg = newLocation.coordinate.latitude;
 NSLog(@"Lat: %g", latDeg);

 float longDeg = newLocation.coordinate.longitude;
 NSLog(@"Lat: %g", longDeg);
}

I want to use the latDeg and longDeg variables.

1
  • But you're using them in the code already. Commented Apr 23, 2010 at 17:28

3 Answers 3

3

Declare latDeg and longDeg as instance variables in your class. Declaring properties for the instance variables and using them for every access of the variable is optional, but recommended.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to reuse them in the same class, you can just declare them after implementation of you class. For example `@implementation LocationClass float latDeg; float longDeg

  • (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { latDeg = newLocation.coordinate.latitude; NSLog(@"Lat: %g", latDeg);

    longDeg = newLocation.coordinate.longitude; NSLog(@"Lat: %g", longDeg); } ` that's enough. Or you can declare them in you AppDelegater and then use them like:

    YourAppDelegate delegate; delegate = (YourAppDelegate) [[UIApplication sharedApplication] delegate]; float currVar = [delegate->longDeg];

Comments

0

a) Make them global variables

b) Make that LocationManager a property of the class you're using it in. You now can add two double-properties to that came class where you might save your latitude or longitude in.

c) Extend your function with another function that takes the lat and lon and writes them to a variable at wherever you need it.

I would highly recommend version b, since it's not only the most simple and cleanest, but it's the only one that does not most likely hurt the OCP at some point.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.