Вот код, все работает отлично, но каждый раз, когда я нажимаю кнопку «Получить мое местоположение», он дважды обновляет местоположение, я не могу найти причину, почему? Я удалил много кода из-под этого, и он все еще делает это, поэтому я знаю, что он находится в этой части где-то. Благодарю.Почему этот код обновляет местоположение дважды каждый раз?
.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
Еще раз спасибо. ;) – Nicoll
Я говорил слишком рано, я снова сделал все приложение (также изменено на - locationManager: didUpdateLocations: но все равно он обновляется дважды! Я вижу его в выходной консоли, и предупреждение о сохранении файла появляется дважды. – Nicoll
Я замечаю, что при использовании симулятора я получаю «Обновление местоположения» в консоли плюс ошибка «Ошибка произошла» (ошибка Cocoa 4.) Я предполагаю, что ошибка связана с тем, что она не может обрабатывать папку var/mobile/Documents, которую я понимаю, но это говорит мне, что он дает мне два обновления: один для обновления местоположения и другое обновление, когда он сохраняет файл! – Nicoll