Я пытаюсь получить координаты пользователей GPS из моего приложения, которое работает нормально, один раз. Когда вызывается вызов, я запускаю код и получаю местоположение - хорошо - но когда я вернусь к представлению, я хочу НОВЫЕ координаты, но я получаю только старые, как будто их обрезают в телефоне.Невозможно получить новое местоположение после остановкиОбновление Местоположение - IOS
Вот мой код:
#import "PostPictureViewController.h"
@interface PostPictureViewController()
@end
@implementation PostPictureViewController
{
CLLocationManager *locationManager;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void) viewDidAppear:(BOOL)animated{
locationManager = [[CLLocationManager alloc] init];
[self getLocation];
}
-(void)getLocation{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Fel" message:@"Error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
NSLog([NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
NSLog([NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
}
// Stop Location Manager
[locationManager stopUpdatingLocation]; //This is screwing it up
locationManager = nil;
}
.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface PostPictureViewController : UIViewController<CLLocationManagerDelegate>
@end
Я думаю, моя проблема заключается в [locationManager stopUpdatingLocation];
линии, где я останавливаю locationManager после получения координат в первый раз. Я пробовал без этой строки, а затем он работает - обновляет координаты, но я не хочу, чтобы locationmanager
посылал мне новые координаты каждую секунду. Мне нужны только один раз за просмотр.
У кого-нибудь есть ключ? Заранее спасибо
Спасибо за ответ, но его не работает на 100%, он обновляет каждый другой раз, и когда я просто пойти на один шаг назад и их вперед (?) представление снова не обновляется – PaperThick
@PaperThick попробуйте сейчас, я отредактировал. Он будет обновлять местоположение каждый раз, когда появится представление, и оно остановится сразу после его получения. Я понял, что ты этого хочешь, правильно? –
Не жаль, та же проблема. Я обновил свой вопрос с помощью некоторого дополнительного кода, но я уверен, что это не важно. – PaperThick