I am working on an iOS app that's focuses on pedestrians in a block of buildings (campus). I have developed a good enough (I believe) user's location update but, I would like anyone more expert than me to give me some tips, advices on accurate location, battery's issues etc. In addition, is it possible to stop the location update and start it again after n seconds? Is a thought that I did in order to save energy. As it is at the moment, app is detecting current location, but, blue dot (user) is still moving around and I don't like that. There is something that can be done?
Below is my didUpdateToLocation method:
App is reading buildings' info from a file (stored on device)
- (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];
}
}