2013-05-06 61 views
3

This screenshot taken with no movement of device 我有一個應用程序,用戶在慢跑或騎自行車時跟蹤他/她的路線,所以我需要完美的位置,所以用戶的路線將是完美的。didUpdateToLocation多次調用

不過,我在這一個問題,

locManager = [[CLLocationManager alloc] init]; 
[locManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation]; 
[locManager setDelegate:self]; 
[locManager startUpdatingLocation]; 

在viewDidLoad中。使用這個didUpdateToLocation方法多次調用時,我只是不移動設備一點點和在地圖上非常奇怪的路線繪製。

我只是不明白爲什麼會發生這種情況,如果我做了一些錯誤或失去了一些東西。

謝謝.......

回答

2

當你第一次啓動定位服務,你會看到一般多個位置更新來無論你是運動或不運動。如果您檢查位置的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; 
} 

底線,擺脫那些不如您下一個準確的位置。如果你願意,你可以在修剪過程中變得更具攻擊性,但是在那裏存在權衡,但希望這可以說明這個想法。

2

您可以設置位置管理者希望的distancefilter這可能會幫助你

locationManager=[[CLLocationManager alloc] init]; 
locationManager.delegate=self; 
locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters; 
locationManager.distanceFilter=10.0; 

[locationManager startUpdatingLocation]; 
15

我用locationManager.distanceFilter = 500; (或左右)//米 以防止多次呼叫發生。只記得打電話給在此之前,你開始更新你的位置

0

添加這個,

[locManager stopUpdatingLocation]; 

到您的updateUserLocation委託方法。

審查下面的代碼片段:

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 
    [_locationManager stopUpdatingLocation];   
} 
0

locationManager.startUpdatingLocation()連續獲取位置和didUpdateLocations方法調用locationManager.startUpdatingLocation前調用幾次, 只需設置爲locationManager.distanceFilter值()的值。

當我設置200做工精細

locationManager.distanceFilter = 200 
    locationManager.startUpdatingLocation()