【问题标题】:didUpdateToLocation called multiple times多次调用 didUpdateToLocation
【发布时间】:2013-05-06 12:12:30
【问题描述】:

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

但是,我有一个问题,

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

在 viewDidLoad 中。当我只是不稍微移动设备并且在地图上非常奇怪的路线绘制时,使用此 didUpdateToLocation 方法多次调用。

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

谢谢.......

【问题讨论】:

    标签: iphone ios cllocationmanager


    【解决方案1】:

    我使用 locationManager.distanceFilter = 500; (左右)//米 以防止发生多个呼叫。请记住在开始更新您的位置之前拨打此电话

    【讨论】:

      【解决方案2】:

      您可以设置位置管理器的距离过滤器希望这可以帮助您

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

      【讨论】:

        【解决方案3】:

        当您第一次启动位置服务时,无论您是否在移动,通常都会看到多个位置更新。如果您检查位置的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 的准确度低于最后一个位置,并且它们之间的距离小于最近位置的准确度,那么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;
        }
        

        最重要的是,只需删除比您拥有的下一个位置更不准确的位置。如果你愿意,你可以在修剪过程中变得更加积极,但也有权衡,但希望这能说明这个想法。

        【讨论】:

          【解决方案4】:
          startUpdatingLocation 
          

          即使位置不变,也会不断更新用户的位置。您只需要根据您的需要构建您的应用程序以处理这些持续更新。

          尝试阅读 Apple 关于此主题的文档。一开始会让人困惑,但还是试试吧。

          http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW1

          【讨论】:

            【解决方案5】:

            我想这就是你需要的。startMonitoringForRegion:desiredAccuracy

            例如,请参阅以下github 链接。

            【讨论】:

            • 感谢 rply,但我需要持续跟踪位置
            【解决方案6】:

            试试 Apple 提供的这个 Bread Crumb 示例代码。

            http://developer.apple.com/library/ios/#samplecode/Breadcrumb/Introduction/Intro.html

            【讨论】:

              【解决方案7】:

              添加这个,

              [locManager stopUpdatingLocation];
              

              进入您的 updateUserLocation 委托方法。

              查看以下代码sn-p:

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

              【讨论】:

                【解决方案8】:

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

                当我设置 200 时工作正常

                    locationManager.distanceFilter = 200
                    locationManager.startUpdatingLocation()
                

                【讨论】:

                  猜你喜欢
                  • 2012-12-03
                  • 1970-01-01
                  • 2014-11-30
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-11-01
                  相关资源
                  最近更新 更多