我正在开发一款iOS应用程序,专注于建筑物(校园)中的行人。我已经开发了一个足够好(我相信)用户的位置更新,但是,我希望比我更专业的人给我一些提示,关于准确位置,电池问题的建议等。此外,是否可以停止位置更新和n秒后再次启动它?是为了节省能源而做的一个想法。就像现在一样,app正在检测当前位置,但是,蓝点(用户)仍在四处移动,我不喜欢这样。有什么可以做的吗?
以下是我的didUpdateToLocation方法:
应用程序正在从文件(存储在设备上)中读取建筑物的信息
- (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];
}
}
答案 0 :(得分:3)
ΓειασουΠαναγιώτη。
You should read the official documentation about location services
这是一个很好的指南,应该涵盖一切。 我将为您快速回顾一下,并向您解释每种可用方法的优缺点,因为我已经为我们的应用程序广泛使用Core Location服务:
在iOS中有三种不同的方式来获取用户位置。
因此,根据您的需要,您必须选择服务。 当然,如果您使用GPS方式,就像您在代码中使用的那样,您应该在获得更新后始终关闭位置更新并再次请求位置,仅在一段时间之后时间,否则你的应用程序将耗尽用户的电池。
当然,您可以随时重新启动用户的位置更新。 如果您需要在应用中的许多视图控制器中进行位置更新,我建议您为位置管理器创建一个单例类!
对于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];
}
}
并确保在用户更改viewController的情况下取消了我们的performSelector,将其添加到:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[NSObject cancelPreviousPerformRequestsWithTarget:self];
}