2014-11-22 4 views
-2

Вот код, все работает отлично, но каждый раз, когда я нажимаю кнопку «Получить мое местоположение», он дважды обновляет местоположение, я не могу найти причину, почему? Я удалил много кода из-под этого, и он все еще делает это, поэтому я знаю, что он находится в этой части где-то. Благодарю.Почему этот код обновляет местоположение дважды каждый раз?

.h файл:

#import <CoreLocation/CoreLocation.h> 
#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UILabel *LatitudeLabel; 
@property (weak, nonatomic) IBOutlet UILabel *LongitudeLabel; 
@property (weak, nonatomic) IBOutlet UILabel *GPSAccuracyLabel; 
@property (weak, nonatomic) IBOutlet UILabel *AltitudeLabel; 
@property (weak, nonatomic) IBOutlet UILabel *VerticalAccuracyLabel; 

- (IBAction)getCurrentLocation:(id)sender; 
@end 

@interface MyLocationViewController : UIViewController <CLLocationManagerDelegate> 
@end 

.m файл:

#import "ViewController.h" 

@interface ViewController() 
@end 

@implementation ViewController { 
CLLocationManager *locationManager; 
} 

- (void)viewDidLoad { 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
locationManager = [[CLLocationManager alloc] init]; 
} 

- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

- (IBAction)getCurrentLocation:(id)sender { 
locationManager.delegate = (id)self; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
[locationManager startUpdatingLocation]; 
} 

#pragma mark - CLLocationManagerDelegate 

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

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
NSLog(@"Location updated: %@", newLocation); 
CLLocation *currentLocation = newLocation; 

if (currentLocation != nil) { 
    _LatitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; 
    _LongitudeLabel.text = [NSString stringWithFormat:@"%.6f", currentLocation.coordinate.longitude]; 
    _GPSAccuracyLabel.text = [NSString stringWithFormat:@"%.2f", currentLocation.horizontalAccuracy]; 
    _AltitudeLabel.text = [NSString stringWithFormat:@"%.2f", currentLocation.altitude]; 
    _VerticalAccuracyLabel.text = [NSString stringWithFormat:@"%.2f", currentLocation.verticalAccuracy]; 
} 

Консоль вывода каждый раз, когда я нажимаю кнопку:

2014-11-22 23:49:37.539 MyLocationDemo[914:60b] Location updated: <+10.16863927,+124.75859298> +/- 10.00m (speed 0.00 mps/course -1.00) @ 22/11/14 11:49:37 pm Philippine Standard Time 

2014-11-22 23:49:37.545 MyLocationDemo[914:60b] Location updated: <+10.16863927,+124.75859298> +/- 10.00m (speed 0.00 mps/course -1.00) @ 22/11/14 11:49:37 pm Philippine Standard Time 

ответ

1

После того, как беглый взгляд на документаций Apple, Я заметил, что метод делегата, который вы используете, - locationManager:didUpdateToLocation:fromLocation: устарел с iOS 6.
Вместо этого вы должны использовать - locationManager:didUpdateLocations:.

Попробуйте заменить код на код ниже и посмотреть, если это делает никакой разницы:

Edit- Я редактировать код ниже, чтобы справиться с двойной звонок Вы получаете.
От вашего поста выше, я вижу, что они почти одновременно, поэтому в основном мы проверим, прошло ли с последнего звонка не менее 1 секунды.
Есть, вероятно, лучшие способы сделать это, но это было как раз на моей голове ...
Не проверял его в Xcode, но, если я не сделал опечатку или что-то еще, она должна работать ,

// ViewController.m 
@interface ViewController() 
@property (nonatomic, strong) NSDate *lastUpdateTime; // Create a property 
@end             // to hold current time 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.lastUpdateTime = [NSDate date]; // In viewDidLoad, 'initialize' it 
              // to get the current time 
} 

- (void)locationManager:(CLLocationManager *)manager 
didUpdateLocations:(NSArray *)locations 
{ 
    NSTimeInterval passedTime = -[self.lastUpdateTime timeIntervalSinceNow]; 
    // Here we are checking how much seconds have passed since our lastUpdateTime 
    // Since lastUpdateTime is in the past, the result will be negative, therefore 
    // the minus sign, so we'll get a positive number 

    if(passedTime < 1) { 
     return; 
    } // Now we check if less than one second have passed. If so, the whole method 
     // will return. If not, it will just continue executing 

    CLLocation *currentLocation = [locations lastObject]; 
    self.lastUpdateTime = [NSDate date]; // Don't forget to update the lastUpdateTime 
              // To hold the new update time 

    if (currentLocation != nil) { 
     NSLog(@"Location updated: %@", currentLocation); 
     _LatitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; 
     _LongitudeLabel.text = [NSString stringWithFormat:@"%.6f", currentLocation.coordinate.longitude]; 
     _GPSAccuracyLabel.text = [NSString stringWithFormat:@"%.2f", currentLocation.horizontalAccuracy]; 
     _AltitudeLabel.text = [NSString stringWithFormat:@"%.2f", currentLocation.altitude]; 
     _VerticalAccuracyLabel.text = [NSString stringWithFormat:@"%.2f", currentLocation.verticalAccuracy]; 
    } 
} 
+0

Еще раз спасибо. ;) – Nicoll

+0

Я говорил слишком рано, я снова сделал все приложение (также изменено на - locationManager: didUpdateLocations: но все равно он обновляется дважды! Я вижу его в выходной консоли, и предупреждение о сохранении файла появляется дважды. – Nicoll

+0

Я замечаю, что при использовании симулятора я получаю «Обновление местоположения» в консоли плюс ошибка «Ошибка произошла» (ошибка Cocoa 4.) Я предполагаю, что ошибка связана с тем, что она не может обрабатывать папку var/mobile/Documents, которую я понимаю, но это говорит мне, что он дает мне два обновления: один для обновления местоположения и другое обновление, когда он сохраняет файл! – Nicoll

0

@ AMI289, ваша идея работала, не более двойной звонок.

Я размещаю окончательный код здесь, если он помогает другим, я только что добавил обратно locationManager = [[CLLocationManager alloc] init];.

// ViewController.m 

#import "ViewController.h" 
@interface ViewController() 
@property (nonatomic, strong) NSDate *lastUpdateTime; // create a property to hold current time. 
@end 

@implementation ViewController { 
CLLocationManager *locationManager; 
} 

- (void)viewDidLoad { 
[super viewDidLoad]; 
self.lastUpdateTime = [NSDate date]; // In viewDidLoad, 'initialize' it to get the current time 
locationManager = [[CLLocationManager alloc] init]; 
} 

- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

- (IBAction)getCurrentLocation:(id)sender { 
locationManager.delegate = (id)self; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
[locationManager startUpdatingLocation]; 
} 
#pragma mark - CLLocationManagerDelegate 

- (void)locationManager:(CLLocationManager *)manager 
didUpdateLocations:(NSArray *)locations 
{ 
NSTimeInterval passedTime = -[self.lastUpdateTime timeIntervalSinceNow]; 
// Here we are checking how much seconds have passed since our lastUpdateTime 
// Since lastUpdateTime is in the past, the result will be negative, therefore 
// the minus sign, so we'll get a positive number 

if(passedTime < 1) { 
    return; 
} // Now we check if less than one second have passed. If so, the whole method 
// will return. If not, it will just continue executing 

CLLocation *currentLocation = [locations lastObject]; 
self.lastUpdateTime = [NSDate date]; // Don't forget to update the lastUpdateTime 
// To hold the new update time 

if (currentLocation != nil) { 
    NSLog(@"Location updated: %@", currentLocation); 
    _LatutideLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; 
    _LongitudeLabel.text = [NSString stringWithFormat:@"%.6f", currentLocation.coordinate.longitude]; 
    _GPSAccuracyLabel.text = [NSString stringWithFormat:@"%.2f", currentLocation.horizontalAccuracy]; 
    _AltitudeLabel.text = [NSString stringWithFormat:@"%.2f", currentLocation.altitude]; 
    _VerticalAccuracyLabel.text = [NSString stringWithFormat:@"%.2f", currentLocation.verticalAccuracy]; 
} 
// Stop Location Manager 
[locationManager stopUpdatingLocation]; 
} 

@end 
+0

О, я вижу, я добавил обратно locationManager = [[CLLocationManager alloc] init]; Испытывает теперь приятеля. – Nicoll