我想每秒跟蹤用戶移動並在Google地圖上顯示它。爲此,我使用下面的代碼。但是這段代碼每秒都會觸發多次。iOS didUpdateLocations停止
首先,我想停止每秒多次調用該方法。然後,當我在某個位置暫停5分鐘時,它應該跟蹤相同的經度和緯度並插入到我的位置數組中 - 我需要的是,當用戶不走幾分鐘時(相同的經度和緯度不現在進入我的位置數組)。
CODE:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//NSLog(@"didUpdateLocations");
for (CLLocation *newLocation in locations) {
//NSLog(@"in foor loop");
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (fabs(howRecent) < 10.0 && newLocation.horizontalAccuracy < 20) {
// update distance
if (self.locations.count > 0) {
self.distance += [newLocation distanceFromLocation:self.locations.lastObject];
CLLocationCoordinate2D coords[2];
coords[0] = ((CLLocation *)self.locations.lastObject).coordinate;
coords[1] = newLocation.coordinate;
MKCoordinateRegion region =
MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 500, 500);
// [self.delegate updateRegion:region withOverLayCoordinates:coords];
}
[self.locations addObject:newLocation];
// NSLog(@"self.locations:: %@",self.locations);
}
}
}
有沒有答案讓你朝着正確的方向?它有幫助你嗎? – Michal