用户的位置和电池的消耗

我正在研究侧重于build筑物(校园)中的行人的iOS应用程序。 我已经开发了一个足够好的(我相信)用户的位置更新,但是,我想任何人比我更专家给我一些提示,build议准确的位置,电池的问题等。此外,是否有可能停止位置更新和n秒后再次启动? 是我为了节能而做的一个想法。 目前,应用程序正在检测当前位置,但蓝点(用户)仍在移动,我不喜欢这样。 有什么可以做的?

以下是我的didUpdateToLocation方法:

应用程序正在从一个文件(存储在设备上)读取build筑物的信息

- (void)viewDidLoad{ [super viewDidLoad]; if ([self checkForInternet]) { _locationManager = [[CLLocationManager alloc] init]; _locationManager.delegate = self; _locationManager.distanceFilter = 10.0f; _locationManager.desiredAccuracy = 20.0f; [_locationManager startUpdatingLocation]; } } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { if (newLocation.horizontalAccuracy > manager.desiredAccuracy) return; [self.mapView removeAnnotations:listOfAnn]; [listOfAnn removeAllObjects]; if ([manager locationServicesEnabled]) { for (NSString* row in rows){ NSArray* cells = [row componentsSeparatedByString:@"\t"]; CLLocationCoordinate2D newCoord; newCoord.latitude = [[cells objectAtIndex:5]floatValue]; newCoord.longitude = [[cells objectAtIndex:6]floatValue]; CLLocation *locB = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude]; CLLocation *centerLoc = [[CLLocation alloc] initWithLatitude:CAMPUS_LATITUDE longitude:CAMPUS_LONGITUDE]; CLLocationDistance borders = [locB distanceFromLocation:centerLoc]; if ((int)round(borders) > 500) { BuildingViewController *newBuilding = [[BuildingViewController alloc] initBuildingWithName:[cells objectAtIndex:2] coordinates:newCoord shortDescription:[cells objectAtIndex:4] image:(NSString*)[cells objectAtIndex:3] inDistance: borders]; if ([[prefs stringForKey:newBuilding.title] isEqualToString:[prefs stringForKey:@"userName"]]) { [newBuilding retrieveID]; [listOfAnn addObject:newBuilding]; } } else{ CLLocation *locA = [[CLLocation alloc] initWithLatitude:newCoord.latitude longitude:newCoord.longitude]; CLLocationDistance distance = [locA distanceFromLocation:locB]; BuildingViewController *newBuilding = [[BuildingViewController alloc] initBuildingWithName:[cells objectAtIndex:2] coordinates:newCoord shortDescription:[cells objectAtIndex:4] image:(NSString*)[cells objectAtIndex:3] inDistance: distance]; if ((int)round(distance) < 100 || [[prefs stringForKey:newBuilding.title] isEqualToString:[prefs stringForKey:@"userName"]]){ [newBuilding retrieveID]; [listOfAnn addObject:newBuilding]; } } } [self.mapView addAnnotations:listOfAnn]; } } 

ΓειασουΠαναγιώτη。

您应该阅读有关位置服务的官方文档

这是一个很好的指导,应该涵盖一切。 我将为您快速回顾一下,并向您解释每种可用方法的优点和缺点,因为我已经为我们的应用程序广泛使用了Core Location服务:

有三种不同的方式来获取iOS中的用户位置。

  1. 正常的GPS定位监视器(优点:非常敏捷和快速更新,缺点:耗尽电池的方式太快)
  2. 重要的定位服务(优点:非常省电,可以从后台启动应用程序,即使在input区域时终止,而不需要后台位置监控任务和权限,缺点:在确定位置和接收位置更新时非常不准确变化)
  3. 区域监控(优点:非常省电,可以从后台启动应用程序,即使在input区域时终止,而不需要后台位置监控任务和权限缺点:获取有关input区域的通知不准确,最多20个区域可以由app监视)

所以根据你想要的精度,你必须select服务。 当然,如果你使用的是GPS方式,就像你在代码中使用的那样, 你应该在获得更新后closures位置更新,并在一段时间后再次请求位置,否则你的应用程序将耗尽用户的电池。

当然,您可以随时重新启动用户的位置更新。 如果您需要在您的应用程序的许多视图控制器中的位置更新,我build议您为位置pipe理器创build一个单身人员类!

至于GPS监控,请在您的CoreLocation委托方法didUpdateToLocation中执行以下操作:

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSDate* eventDate = newLocation.timestamp; NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; //prevent Location caching... (only accepts cached data 1 minute old) if( abs(howRecent) < 60.0) { [self.locationManager stopUpdatingLocation]; //capture the location... (this is your code) //update location after 90 seconds again [self performSelector:@selector(startUpdatingLocation) withObject:nil afterDelay:90]; } } 

并再次更新位置:

 - (void)startUpdatingLocation { if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorized || [CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined) { self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; [locationManager startUpdatingLocation]; } } 

并确保我们取消了我们的performSelector,以防用户更改viewController时,将其添加到:

 - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [NSObject cancelPreviousPerformRequestsWithTarget:self]; }