2

在 Xcode 4.5 苹果引入了苹果新的地图。我的应用程序需要地图服务。而且我注意到在我的应用程序中它会显示错误的当前位置,直到您删除应用程序并重新打开它才会显示正确的当前位置(有时不会)。顺便提一下,当它显示错误的当前位置时,我已连接到 4G。有没有办法修复这个错误,因为我的应用程序非常需要正确的位置。提前致谢。如果您可以提供代码,那将有很大帮助...编辑: Apple Map App 中的相同问题

我的代码:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    {

    }

    if (!oldLocation)
        totalDistance = 0.0;
    else
        totalDistance += [newLocation distanceFromLocation:oldLocation];

}
4

3 回答 3

3

苹果文档中的旧方法似乎仍然适用于 iOS6(在我的活动应用程序中没有注意到这一点(它通过 gps 跟踪用户的路线))

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    if (locationAge > 5.0) return;
    if (newLocation.horizontalAccuracy < 0) return; 

    // proceed with coords here
}

讨论中的更新:计算总距离和当前距离可以这样完成(不包括一些小东西):

// somewhere at the top
CLLocation* lastUsedLocation = nil; // last location used in calculation
CLLocation* pointA = nil;  // start of the track
double totalDistance = 0;  // total distance of track
double currentDistance = 0; // distance between startTrack point and current location
...

// when you start updating location:
- (void) startTracking {
    lastUsedLocation = nil;
    pointA = nil;
    totalDistance = 0;
    currentDistance = 0;
    [locationManager startUpdatingLocation];
}
...


// location update callback
 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; 
    if (locationAge > 5.0) return;  // filter cached
    if (newLocation.horizontalAccuracy < 0) return; // filter invalid

    if(!pointA) pointA = [newLocation retain]; 

    if(lastUsedLocation) 
    { 
        totalDistance += [newLocation distanceFromLocation:lastUsedLocation]; 
    } 
    currentDistance = [pointA distanceFromLocation:newLocation]; 
    [lastUsedLocation release]; 
    lastUsedLocation = [newLocation retain]; 
}

如果您需要故意关闭后台位置的选项,您可以手动禁用它,例如:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    if(backgroundLocationDisabled)
    {
        [locationManager stopUpdatingLocation];
        // additional stuff
    }
}
于 2012-11-28T06:26:18.267 回答
1

我注意到 Xcode 4.4+ 存在同样的问题。该问题仅在 Xcode 中发生(随机发生,或者看起来如此):如果您将应用程序上传到 App Store,这不再是问题。同时,请提交一个错误。

于 2012-11-28T06:29:35.457 回答
0

您应该检查时间戳..如果我正确理解您的应用程序逻辑,您可以执行以下操作 -

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {

NSDate* time = newLocation.timestamp;
    NSTimeInterval timePeriod = [time timeIntervalSinceNow];
    if(timePeriod < SOME_MINIMUM_TIME_PERIOD ) { //for eg: timePeriod < 1.0
               totalDistance += [newLocation distanceFromLocation:oldLocation];
    } else {
        // skip.. or do what you do on a 'location-not-updated' event
    }
}
于 2012-11-28T06:19:09.783 回答