I am trying to get the user current latest location on the didUpdateToLocation:newLocation:fromLocation:oldLocation:
method and store it in my ivar CLLocation *userCurrentLocation
I get this error when I tried to read userCurrentLocation
-[NSCFNumber coordinate]: unrecognized selector sent to instance 0x586b220
- (void) locationManager:(CLLocationManager *) manager
didUpdateToLocation:(CLLocation *) newLocation
fromLocation:(CLLocation *) oldLocation {
fromLocation:(CLLocation *) oldLocation {
if (oldLocation == nil) { // first time loading?
SVGeocoder *geocodeRequest = [[SVGeocoder alloc]
initWithCoordinate:CLLocationCoordinate2DMake(newLocation.coordinate.latitude, newLocation.coordinate.longitude)];
[geocodeRequest setDelegate:self];
[geocodeRequest startAsynchronous]; // reverse geocoding to get annotation coordinates to be placed.
[geocodeRequest release];
isCurrentLocation = YES; //flag that next annotation placed is current location
}
userCurrentLocation = newLocation;
NSLog(@"didUpdateToLocation - Lat:%f Long:%f", userCurrentLocation.coordinate.latitude, userCurrentLocation.coordinate.longitude); // verified that userCurrentLocation latitude is correct at this point.
}
Toolbar button to show user current location
-(IBAction) showLocation:(id) sender {
NSLog(@"Lat:%f Long:%f", userCurrentLocation.coordinate.latitude, userCurrentLocation.coordinate.longitude); // crashes upon calling this statement. Why?
NSLog(@"Show Location Called");
SVGeocoder *geocodeRequest = [[SVGeocoder alloc]
initWithCoordinate:CLLocationCoordinate2DMake(userCurrentLocation.coordinate.latitude, userCurrentLocation.coordinate.longitude)];
[geocodeRequest setDelegate:self];
[geocodeRequest startAsynchronous];
[geocodeRequest release];
}
I did set the property and synthesize userCurrentLocation as such:
MapViewController.h
@interface MapViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate> {
CLLocation *userCurrentLocation;
}
@property (nonatomic, retain) CLLocation *userCurrentLocation;
@end
MapViewController.m
@synthesize userCurrentLocation;
/* EDITED */
- (void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
userCurrentLocation = [[CLLocation alloc] init];
}
Your userCurrentLocation
is autoreleased after the - (void) locationManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation *) oldLocation
method finished.
Try use self.userCurrentLocation = newLocation;
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With