1

我在打电话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 ....        
}

我究竟做错了什么?

4

5 回答 5

1

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

locationManager:didUpdateToLocation:fromLocation:已弃用,因此您可以期望它不会总是被使用。

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

于 2013-09-10T10:08:02.740 回答
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;
}
于 2013-09-10T10:14:54.253 回答
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 个。在我的示例中,我将最后一个位置放在我的助手中。

于 2013-09-10T10:11:16.063 回答
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];
}
于 2013-09-10T10:12:13.423 回答
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];
}
于 2013-09-10T10:21:32.560 回答