我正在开发一款步行轨迹应用,但遇到了一个问题:当应用在后台时,位置如何更新?我将位置更新和计时器作为绘制地图上的路径。请建议我如何处理。 以下是我的代码:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if(!newLocation) return;
if ((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) &&
(oldLocation.coordinate.longitude != newLocation.coordinate.longitude))
{
// to draw path as new location find
jogPoint = [[JogPoint alloc] initWithCenterCoordinate:newLocation.coordinate];
[jogPoints addObject:jogPoint];
if([jogPoints count] >= 5) {
[self.mapView addOverlay:jogPoint];
CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:oldLocation.coordinate.latitude longitude:oldLocation.coordinate.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
// returns distance in meters
distnc+=[loc1 distanceFromLocation:loc2] ;
distanceLabel.text=[NSString stringWithFormat:@"%lf",distnc];
// jogInfo.distance += ([loc1 distanceFromLocation:loc2]) * 0.000621371192;
// jogInfo.eclapsedTime = (CFAbsoluteTimeGetCurrent() - startTime) * 1000 * 60;
}
}
}
stopUpdatingLocation
,这样它将继续调用didUpdateToLocation
方法。 - iPatel