优化CLLocationManager/CoreLocation以在iPhone上更快地检索数据点

17 iphone cocoa-touch core-location

我目前正在使用CoreLocation + CLLocationManager来将用户当前位置与N个其他位置进行比较.我面临的问题是我正在处理位置彼此接近的城市区域,所以我需要在不牺牲太多时间的情况下准确地指出用户的位置.我的计算是准确的,问题是,收集10个样本需要太长时间.我将在下面粘贴我的过滤器/精确度相应的代码.如果有人可以评论我如何加快速度,那就太好了.直到我加速,它在我的应用程序中相当无用,因为它目前需要大约3-4分钟来收集信息,这是我的目标人群不接受的持续时间.

代码看起来像这样:

[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if (newLocation.horizontalAccuracy > 0.0f &&
        newLocation.horizontalAccuracy < 120.0f) { // roughly an accuracy of 120 meters, we can adjust this.

    [myLocs addObject:newLocation];
}
Run Code Online (Sandbox Code Playgroud)

感谢任何优化技巧,以加快这一进程.

Fro*_*st 48

我在iPhone上的CLLocationManager也遇到了很多问题.从不同的应用程序,反复试验,这是我已经想到的;

1)对locationManager使用单例类,只有一个实例,请按照以下示例操作:

Apple的LocateMe示例

Tweetero:http://tweetero.googlecode.com/svn/trunk

我认为你只能运行一个位置管理器,iPhone中只有一个GPS芯片,因此如果你启动多个位置管理器对象,它们就会发生冲突,并且你从GPS位置管理器那里得到错误,说它无法更新.

2)要非常小心你如何更新locationManager.desiredAccuracylocationManager.distanceFilter

在FlipsideViewController中按照Apple的代码中的示例进行操作:

[MyCLController sharedInstance].locationManager.desiredAccuracy = accuracyToSet;
[MyCLController sharedInstance].locationManager.distanceFilter = filterToSet;
Run Code Online (Sandbox Code Playgroud)

这种方法有效,并且不会导致错误.如果在主委托循环中更新desiredAccuracy或distanceFilter:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation  fromLocation:(CLLocation *)oldLocation
Run Code Online (Sandbox Code Playgroud)

核心位置可能会抱怨它无法更新值,并给您错误.另外,只使用desiredAccuracyApple提供的常量,而不使用其他常量;

kCLLocationAccuracyBest,kCLLocationAccuracyNearestTenMeters,
kCLLocationAccuracyHundredMeters,kCLLocationAccuracyKilometer,
kCLLocationAccuracyThreeKilometers

使用其他值可能会给您带来locationManager的错误.因为distanceFilter你可以使用任何值,但要注意人们已经声明它有问题,默认的主要值是:-1 =最好,我已经使用10.0,它似乎没问题.

3)要强制进行新的更新,请startUpdates像在Tweetero中一样调用方法,这会强制locationManager执行此操作,它应该尝试为您获取新值.

4)核心位置委托例程很难获得准确的更新.当您的应用程序首次初始化时,您的准确度可能会超过1000米或更长,因此一次尝试不会这样做.初始化后的三次尝试通常会让你达到47左右,这是好的.请记住,每次连续尝试都需要更长时间才能提高准确性,因此很快就会变得昂贵.这就是你需要做的事情:只有在最近时才取新值.如果新位置的准确度略高于新位置,或者如果新位置的精度<50米,则取新位置或如果新位置尝试已经超过3或5并且精度<150米并且位置已经改变,那么这是一个新的位置并且设备移动所以即使它的准确性更差也要取这个值.这是我的位置代码:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation  *)newLocation  fromLocation:(CLLocation *)oldLocation
{
    locationDenied = NO;

    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"UseLocations"])
    {
        [self stopAllUpdates];
        return;
    }

    NSDate* eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = abs([eventDate timeIntervalSinceNow]);
    attempts++;

    if((newLocation.coordinate.latitude != oldLocation.coordinate.latitude) && (newLocation.coordinate.longitude != oldLocation.coordinate.longitude))
        locationChanged = YES;
        else
            locationChanged = NO;

    #ifdef __i386__
            //Do this for the simulator since location always returns Cupertino
            if (howRecent < 5.0)
    #else
                // Here's the theory of operation
                // If the value is recent AND
                // If the new location has slightly better accuracy take the new location OR
                // If the new location has an accuracy < 50 meters take the new location OR
                // If the attempts is maxed (3 -5) AND the accuracy < 150 AND the location has changed, then this must be a new location and the device moved
                // so take this new value even though it's accuracy might be worse
                if ((howRecent < 5.0) && ( (newLocation.horizontalAccuracy < (oldLocation.horizontalAccuracy - 10.0)) || (newLocation.horizontalAccuracy < 50.0)
                                          || ((attempts >= attempt) && (newLocation.horizontalAccuracy <= 150.0) && locationChanged)))
    #endif
                {
                    attempts = 0;
                    latitude = newLocation.coordinate.latitude;
                    longitude = newLocation.coordinate.longitude;
                    [currentLocation release];
                    currentLocation = [newLocation retain];
                    goodLocation = YES;

                    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateLocationNotification" object: nil];

                    if (oldLocation != nil) {
                        distanceMoved = [newLocation getDistanceFrom:oldLocation];
                        currentSpeed = newLocation.speed;
                        distanceTimeDelta = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];
                    }
                }
    // Send the update to our delegate
    [self.delegate newLocationUpdate:currentLocation];
}
Run Code Online (Sandbox Code Playgroud)

如果你有iPhone 3GS获得标题更新要容易得多,这里的代码与上面相同的模块:

//This is the Heading or Compass values
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
    if (newHeading.headingAccuracy > 0)
    {
        [newHeading retain];
        // headingType = YES for True North or NO for Magnetic North
        if(headingType) {
            currentHeading = newHeading.trueHeading;
        } else {
            currentHeading = newHeading.magneticHeading;
        }

        goodHeading = YES;
        [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateHeadingNotification" object: nil];

    }
}
Run Code Online (Sandbox Code Playgroud)

希望这有助于人们!