我正在重构旧代码,以便让事情变得干净,尤其是地理定位部分,现在它有点乱。
我正在记录每个地理位置方法,我发现#include <ctype.h> //header for isspace
while ( scanf ( "%c", &letter) == 1) {
if ( isspace ( letter)) {
if ( letter == '\n') {
break;
}
}
else {
ungetc ( letter, stdin);//replace letter in input stream
if ( scanf ( "%19s", array[i]) == 1)
{
i++;
if ( i >= 20) {
break;
}
}
}
}
被称为太多时间。
以下是我的Splashscreen的一些代码:
didUpdateLocations
以下是- (void)viewDidLoad
{
[super viewDidLoad];
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
[self startLocationRetrieval];
}
方法
startLocationRetrieval
以下是CLLocation委托方法
-(void) startLocationRetrieval
{
if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
[_locationManager requestWhenInUseAuthorization];
if ([CLLocationManager locationServicesEnabled]) {
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[_locationManager startUpdatingLocation];
}else {
_isDataLoaded = YES;
[self loadHomeView];
}
我处理位置变化的方法
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
DDLogError(@"GEOLOC CLLocationManager didFailWithError: %@", error);
[self loadHomeView];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
DDLogInfo(@"GEOLOC Splashscreen didUpdateLocations -> %@", locations);
[self manageNewLocation:[locations lastObject]];
}
loadHomeView方法只是触发performSegueWithIdentifier以转到HomePage。
-(void) manageNewLocation:(CLLocation *)location
{
_coordinate = location;
DDLogDebug(@"GEOLOC Splash manageNewLocation = %@", _coordinate);
[self loadHomeView];
}
?我发现了类似的问题IOS didUpdateLocations。但stopUpdatingLocation
正确的方式,似乎很笨拙。
答案 0 :(得分:0)
提取位置后,您需要使用[manager stopUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
[manager stopUpdatingLocation];
manager = nil;
DDLogInfo(@"GEOLOC Splashscreen didUpdateLocations -> %@", locations);
[self manageNewLocation:[locations lastObject]];
}