我试图在didUpdateToLocation:newLocation:fromLocation:oldLocation:
方法中获取用户当前最新位置并将其存储在我的实例变量CLLocation *userCurrentLocation
中
当我尝试读取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.
}
工具栏按钮,显示用户当前位置
-(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];
}
我已经设置属性并合成了userCurrentLocation,代码如下:
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];
}
SVGeocoder
),而不是苹果的MKReverseGeoder
。一切都很顺利,只是似乎在IBAction中读取userCurrentLocation
对象会导致应用程序崩溃,我无法弄清原因。 - Gavin