So I have a model object, which is a named 'location' which has a set of properties on. One of which is 'distanceFromUserLocation'. Obviously, to know this it needs to know the user's location, so in the model object I am calculating the user's current location.
This also sets a CLLocation property on this model every time on the didUpdateToLocation method.
I also have an initWithDictionary method on the model which sets all the values for properties on the location model.
code below for my model:
@synthesize name = _name;
@synthesize entryLocation = _entryLocation;
@synthesize address = _address;
@synthesize contactNumber = _contactNumber;
@synthesize distanceFromUserLocation = _distanceFromUserLocation;
@synthesize itemDescription = _itemDescription;
@synthesize locationManager = _locationManager;
@synthesize userLocation = _userLocation;
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (newLocation != nil) {
self.userLocation = newLocation;
NSLog(@"lat: %@ lon: %@ ", [NSString stringWithFormat:@"%.7f", newLocation.coordinate.latitude], [NSString stringWithFormat:@"%.7f", newLocation.coordinate.longitude]);
}
[manager stopUpdatingLocation];
}
-(id)initWithDictionary:(NSDictionary*) dictionary
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
self = [super init];
if (self)
{
self.name = [dictionary valueForKey:@"Name"];
self.address = [dictionary valueForKey:@"Address"];
self.contactNumber = [dictionary valueForKey:@"ContactNumber"];
self.itemDescription = [dictionary valueForKey:@"Description"];
double lat = [[dictionary valueForKey:@"Latitude"] doubleValue];
double lon = [[dictionary valueForKey:@"Longitude"] doubleValue];
self.entryLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
self.distanceFromUserLocation = [self.userLocation distanceFromLocation:self.entryLocation];
}
return self;
}
The problem I have, is that in the didUpdateToLocation, I am setting the 'self.currentLocation' property to the value of newLocation. I then log the lat and the long to make sure i actually have a value, which I do.
However, when i then initialize an instance of the 'Location' model from my controller class and then log the value for currentLocation on that model object:
NSLog(@"%@", location.currentLocation);
I get (null) in the log.
If i try:
NSLog(@%@", location.currentLocation.coordinate.latitude);
I get a EXC_BAD_ACCESS error.
I am successfully getting the current location in the didUpdateToLocation and loggint it, but then why when I set my currentLocation property to the value of newLocation does it not get retained?
Any help with this would be greatly appreciated :)
So it turns out the EXC_BAD_ACCESS error was down to what joost and JBat100 suggested. Just down to using %@ instead of %f. I did however figure out why I couldn't ever get the user location value out of the property on the controller. The methods are called asynchronously and the forRowAtIndexPath method was rendering the table cells BEFORE user location could ever be found, so the pointer to userlocation was pointing to nothing when it was called.
I solved it by adding a view with an activity indicator which finds the user location, saves it to a property and THEN pushes the view with the table in onto the screen, setting it's userlocation property that way so that the property is never empty :)
Thanks for the help everyone!