0

I have the following code:

- (void) viewWillAppear {
    [self startSignificantChangeUpdates];
}



- (void)startSignificantChangeUpdates
{


    if ([CLLocationManager locationServicesEnabled])
    {
        NSLog(@"start significant changes");

        if (nil == locationManager)
            locationManager = [[CLLocationManager alloc] init];

        locationManager.delegate = self;
        [locationManager startMonitoringSignificantLocationChanges];
    }
}


problem is that the location manage is not  calling its delegate function 
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

What can I do?

4

3 回答 3

1

在您的 .h 文件中:

#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController <CLLocationManagerDelegate>

在您的 .m 文件中:

@implementation ViewController {
    // This is a private variable, used within this file only
    CLLocationManager *locationManager;
}

-(void)viewDidLoad {
    locationManager = [[CLLocationManager alloc] init];
}

-(void)viewWillAppear {
    // If the delegate is still not being set, try putting this code into viewDidAppear
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

-(void)locationManger:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"didFailWithError: %@", error);
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    NSLog(@"didUpdateToLocation: %@", [locations lastObject]);
    CLLocation *currentLocation = [locations lastObject];
    [locationManager stopUpdatingLocation];
}

有关获取用户位置的更多信息,请查看本指南,我用它来在我的应用程序中实现位置:http: //www.appcoda.com/how-to-get-current-location-iphone-user/

于 2013-07-30T20:27:04.673 回答
0

请替换此行

[locationManager startMonitoringSignificantLocationChanges]; 

使用此代码

[locationController.locationManager startUpdatingLocation];

然后检查

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
}
于 2013-08-08T04:52:03.147 回答
0

你如何测试这个?如果你在模拟器中运行它,你可以去 Debug->Location->Freeway Drive。您可以在 locationManager:didUpdateToLocation 方法中放置一个断点,甚至可以放置一条日志语句来记录当前位置坐标。你应该看到它起作用了。

于 2013-07-30T15:47:29.883 回答