當你第一次啓動定位服務,你會看到一般多個位置更新來無論你是運動或不運動。如果您檢查位置的horizontalAccuracy
,他們會發現,當它「變暖」時,它會顯示一系列精度越來越高(即越來越小的horizontalAccuracy
值)的位置,直到達到靜止。
您可以忽略這些初始位置,直到horizontalAccuracy
降至特定值以下。或者,更好的是,在啓動期間,如果(a)新位置和舊位置之間的距離小於舊位置的horizontalAccuracy
,並且(b)如果新位置的horizontalAccuracy
較少,則可以忽略先前位置比以前的位置。
例如,假設你保持CLLocation
對象的數組,以及到最後的繪製路徑的引用:
@property (nonatomic, strong) NSMutableArray *locations;
@property (nonatomic, weak) id<MKOverlay> pathOverlay;
此外,讓我們假設你的位置更新程序是公正加入的位置陣列,然後指示該路徑應該被重畫:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(@"%s", __FUNCTION__);
CLLocation* location = [locations lastObject];
[self.locations addObject:location];
[self addPathToMapView:self.mapView];
}
然後addPathToMapView
因此可以去除第二從最後一個位置開始,如果它不如最後一個位置精確,並且它們之間的距離小於最近的位置精度。
- (void)addPathToMapView:(MKMapView *)mapView
{
NSInteger count = [self.locations count];
// let's see if we should remove the penultimate location
if (count > 2)
{
CLLocation *lastLocation = [self.locations lastObject];
CLLocation *previousLocation = self.locations[count - 2];
// if the very last location is more accurate than the previous one
// and if distance between the two of them is less than the accuracy,
// then remove that `previousLocation` (and update our count, appropriately)
if (lastLocation.horizontalAccuracy < previousLocation.horizontalAccuracy &&
[lastLocation distanceFromLocation:previousLocation] < lastLocation.horizontalAccuracy)
{
[self.locations removeObjectAtIndex:(count - 2)];
count--;
}
}
// now let's build our array of coordinates for our MKPolyline
CLLocationCoordinate2D coordinates[count];
NSInteger numberOfCoordinates = 0;
for (CLLocation *location in self.locations)
{
coordinates[numberOfCoordinates++] = location.coordinate;
}
// if there is a path to add to our map, do so
MKPolyline *polyLine = nil;
if (numberOfCoordinates > 1)
{
polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfCoordinates];
[mapView addOverlay:polyLine];
}
// if there was a previous path drawn, remove it
if (self.pathOverlay)
[mapView removeOverlay:self.pathOverlay];
// save the current path
self.pathOverlay = polyLine;
}
底線,擺脫那些不如您下一個準確的位置。如果你願意,你可以在修剪過程中變得更具攻擊性,但是在那裏存在權衡,但希望這可以說明這個想法。
來源
2013-05-06 12:26:32
Rob
感謝rply,但我需要不斷地跟蹤位置 – iBhavik 2013-05-06 12:23:05
查看更新。 – Ayush 2013-05-06 12:32:13