Iphone 多次调用didUpdateToLocation

Iphone 多次调用didUpdateToLocation,iphone,ios,cllocationmanager,Iphone,Ios,Cllocationmanager,我有一个应用程序,其中用户跟踪他/她的路线时慢跑或骑自行车,所以我需要完美的位置,所以用户的路线将是完美的 但是,我有一个问题 locManager = [[CLLocationManager alloc] init]; [locManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation]; [locManager setDelegate:self]; [locManager startUpdatingLocation]; 在

我有一个应用程序,其中用户跟踪他/她的路线时慢跑或骑自行车,所以我需要完美的位置,所以用户的路线将是完美的

但是,我有一个问题

locManager = [[CLLocationManager alloc] init];
[locManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];
[locManager setDelegate:self];
[locManager startUpdatingLocation];
在viewDidLoad中。使用这个didUpdateLocation方法调用多次,当我只是不移动设备一点,在地图上画出非常奇怪的路线时

如果我做错了什么或错过了什么,我就是不明白为什么会发生这种情况


谢谢……

我想这就是你需要的

例如,请参见以下链接

将持续更新用户的位置,即使位置不变。你只需要根据自己的需要构建应用程序来处理这些持续更新

试着阅读苹果关于这个主题的文档。一开始很混乱,但还是要试试看


当您第一次启动位置服务时,无论您是否移动,通常都会看到多个位置更新。如果您在位置进入时检查其
水平精度
,您会看到,在“预热”过程中,它将显示一系列精度越来越高的位置(即越来越小的
水平精度
值),直到达到静止状态

您可以忽略这些初始位置,直到
水平精度
低于某个值。或者,更好的是,在启动过程中,如果(a)新位置和旧位置之间的距离小于旧位置的
水平精度
,并且(b)如果新位置的
水平精度
,则可以忽略以前的位置


例如,假设您正在维护一个
CLLocation
对象数组,以及对最后绘制的路径的引用:

@property (nonatomic, strong) NSMutableArray *locations;
@property (nonatomic, weak) id<MKOverlay> pathOverlay;
因此,如果第二个位置的精度低于最后一个位置,并且它们之间的距离小于最近位置的精度,则
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;
}
-(void)addPathToMapView:(MKMapView*)mapView
{
NSInteger计数=[self.locations计数];
//让我们看看是否应该删除倒数第二个位置
如果(计数>2)
{
CLLocation*lastLocation=[self.locations lastObject];
CLLocation*previousLocation=self.locations[count-2];
//如果最后一个位置比前一个位置更精确
//如果两者之间的距离小于准确度,
//然后删除“previousLocation”(并适当地更新我们的计数)
if(lastLocation.HorizontalAccurance1)
{
polyLine=[MKPolyline polylineWithCoordinates:coordinates count:numberOfCoordinates];
[地图视图添加覆盖:多段线];
}
//如果绘制了以前的路径,请将其删除
if(自身病理学研究)
[mapView removeOverlay:self.pathOverlay];
//保存当前路径
self.pathOverlay=多段线;
}

最重要的是,只要去掉比下一个位置更不准确的位置就行了。如果你愿意的话,你可以在修剪过程中变得更加激进,但这是一种折衷,但希望这能说明这个想法。

试试苹果提供的这个面包屑示例代码


您可以设置location manager的distancefilter,希望这对您有所帮助

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

 [locationManager startUpdatingLocation];

我使用locationManager.distanceFilter=500;(大约)//米 以防止发生多个呼叫。只需记住在开始更新位置之前调用此按钮即可添加此按钮

[locManager stopUpdatingLocation];
进入
updateUserLocation
delegate方法

查看以下代码段:

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

locationManager.startUpdatingLocation()连续获取位置,并多次调用didUpdateLocations方法, 在调用locationManager.startUpdatingLocation()之前,只需设置locationManager.distanceFilter值

当我设置200时,工作正常

    locationManager.distanceFilter = 200
    locationManager.startUpdatingLocation()

感谢rply,但我需要持续跟踪位置
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    [_locationManager stopUpdatingLocation];        
}
    locationManager.distanceFilter = 200
    locationManager.startUpdatingLocation()