1

私は呼び出しCLLocationManagerており、そのデリゲート メソッドも呼び出しています。しかし、私の問題は、1 km 移動した後に新しい場所を更新していないことです。

これが私のコードです:

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

場所の変更の確認とデリゲート メソッドの呼び出しを開始するには、場所マネージャーを呼び出す必要がstartUpdatingLocationあります。startMonitoringSignificantLocationChanges

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

これを使用してアプリ内でこれを行いました

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)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
   // this method is calling after every 1 sec interval time..
}

iOS 6 以降で使用されます。

The second one is deprecated since iOS 6 and was used before 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 に答える