我正在尝试在didUpdateToLocation:newLocation:fromLocation:oldLocation:方法上获取用户当前的最新位置,并将其存储在我的i​​var 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];
}

最佳答案

userCurrentLocation方法完成后,您的- (void) locationManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation *) oldLocation将自动释放。

尝试改用self.userCurrentLocation = newLocation;

关于iphone - 在MKMapView上获取用户当前位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7227459/

10-09 10:16