位置管理器未更新位置

时间:2013-07-30 15:42:10

标签: ios cllocationmanager cllocation

我有以下代码:

- (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 {

我该怎么办?

3 个答案:

答案 0 :(得分: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/

答案 1 :(得分:0)

你是如何测试的?如果您在模拟器中运行它,您可以转到Debug-&gt; Location-&gt; Freeway Drive。您可以在locationManager:didUpdateToLocation方法中放置一个断点,甚至可以使用日志语句来记录当前位置坐标。你应该看到它有效。

答案 2 :(得分:0)

请替换此行

[locationManager startMonitoringSignificantLocationChanges]; 

使用此代码

[locationController.locationManager startUpdatingLocation];

然后检查

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
}
相关问题