CLLocationManager不更新新位置

时间:2013-09-10 10:00:44

标签: iphone ios ios6

我正在调用CLLocationManager并且它也在调用它的委托方法。但我的问题是,旅行1公里后没有更新其新位置。

这是我的代码:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationTimer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self    selector:@selector(updateLocation1:) userInfo:nil repeats:YES];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;

// Method did update location - Update location when location change

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
   // this method is calling after every 1 sec interval time..
}

-(void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
      fromLocation:(CLLocation *)oldLocation 
{
     // this method is not calling once also after travelling around a km ....        
}

我做错了什么?

5 个答案:

答案 0 :(得分:1)

您应该在位置管理器上调用startUpdatingLocationstartMonitoringSignificantLocationChanges,以便开始检查位置更改并调用您的委托方法。

locationManager:didUpdateToLocation:fromLocation:已弃用,因此您可以预期不会一直使用它。

如果正在调用locationManager:didUpdateLocations:,那么您将收到位置更新。

答案 1 :(得分:1)

- (void)viewDidLoad
{
    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLLocationAccuracyKilometer;
    [locationManager startUpdatingLocation];

    CLLocation *location = [locationManager location];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    //[manager stopUpdatingLocation];

    CLLocationCoordinate2D coordinate_currentlocation = [newLocation coordinate];

    float latitude_current = newLocation.coordinate.latitude;
    float longitude_current = newLocation.coordinate.longitude;
}

答案 2 :(得分:0)

你的第一种方法

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
   // this method is calling after every 1 sec interval time..
}
从iOS 6开始使用

第二个版本自iOS 6开始被弃用,并且在iOS 6之前使用。您可以通过添加帮助程序方法,使用这两种方法,具体取决于运行应用程序的设备上的系统版本。

- (void) locationManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation *) oldLocation
{
    [self locationManager:manager helperForLocation:newLocation];
}

- (void) locationManager:(CLLocationManager *) manager didUpdateLocations:(NSArray *) locations
{
    if([locations count] > 0)
    {
        [self locationManager:manager helperForLocation:[locations objectAtIndex:[locations count] - 1]];
    }
}

- (void) locationManager:(CLLocationManager *) manager helperForLocation:(CLLocation *) newLocation
{
        // your code what to do with location goes here
}

iOS 6检索到的位置包含在列表中,可能超过1.在我的示例中,我将最后一个放到我的帮助器中。

答案 3 :(得分:0)

我已经在我的应用程序中使用此

完成了此操作
myLocationManager = [[CLLocationManager alloc] init];
myLocationManager.delegate = self;
myLocationManager.desiredAccuracy = kCLLocationAccuracyBest;    
[myLocationManager startUpdatingLocation];


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                           initWithTitle:@"Error" message:@"Failed to Get Your Location, Please turn on GPS and Restart The Application"delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
    [errorAlert release];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }

    // Stop Location Manager
    [myLocationManager stopUpdatingLocation];
}

答案 4 :(得分:0)

-(void)postCurrentLocationOfDevice
{

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];
    self.locationManager.delegate = self;

}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
   CURRENT_LOCATION = [locations objectAtIndex:0];
    [self.locationManager stopUpdatingLocation];

}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    CURRENT_LOCATION = newLocation;
    [self.locationManager stopUpdatingLocation];
}