在MKMapView上获取用户当前位置

5

我试图在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];
}
4个回答

5
- (void)viewDidLoad {
    mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,46,320,370)];
    [mapView setDelegate:self];
    [mapView setShowsUserLocation:TRUE];    
    [self.view addSubview:mapView];
    [mapView setMapType:MKMapTypeStandard];

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    if(locationManager.locationServicesEnabled)
    {
        [locationManager startUpdatingLocation];
    }
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    LatNLon = newLocation.coordinate;
    NSLog(@"%f   %f",LatNLon.latitude,LatNLon.longitude);
    MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:LatNLon];
    [geocoder setDelegate:self];
    [geocoder start];
    isVisit=0;
    [locationManager stopUpdatingLocation];
}   

我已经在viewDidLoad中做了你所做的事情。至于反向地理编码,我使用了另一个类(SVGeocoder),而不是苹果的MKReverseGeoder。一切都很顺利,只是似乎在IBAction中读取userCurrentLocation对象会导致应用程序崩溃,我无法弄清原因。 - Gavin
我觉得你没有获取到当前用户的位置,所以应用程序才会崩溃。 - Rahul Juyal

5

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

建议使用 self.userCurrentLocation = newLocation; 替代。


0
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *currentUserLocation = [locations lastObject];
NSLog(@"lat%f - lon%f", currentUserLocation.coordinate.latitude, currentUserLocation.coordinate.longitude);

CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = currentUserLocation.coordinate.latitude;
zoomLocation.longitude= currentUserLocation.coordinate.longitude;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.2*METERS_PER_MILE, 0.2*METERS_PER_MILE);
[mapView setRegion:viewRegion animated:YES];
[locationManager stopUpdatingLocation];

}


0
这是UIScrollView中UIPageControl的代码 我们在使用UIScrollView的代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int pageIndex=scrollView.contentOffset.x / scrollView.frame.size.width;
    self.pageCntrl.currentPage=pageIndex;
}

建议您扩展此答案,更好地解释为什么以及如何回答这个问题。 - brandonscript

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接