OStack程序员社区-中国程序员成长平台

标题: ios - Xcode 4.5 中的当前位置错误 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:50
标题: ios - Xcode 4.5 中的当前位置错误

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

我的代码:

- (void)locationManagerCLLocationManager *)manager
    didUpdateToLocationCLLocation *)newLocation
           fromLocationCLLocation *)oldLocation {
    {

    }

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

}



Best Answer-推荐答案


Apple 文档中的旧方法在 iOS6 中似乎仍然有效(在我的事件应用中没有注意到这一点(它通过 gps 跟踪用户的路线))

- (void)locationManagerCLLocationManager *)manager didUpdateToLocationCLLocation *)newLocation fromLocationCLLocation *)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)locationManagerCLLocationManager *)manager didUpdateToLocationCLLocation *)newLocation fromLocationCLLocation *)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)applicationDidEnterBackgroundUIApplication *)application {
    if(backgroundLocationDisabled)
    {
        [locationManager stopUpdatingLocation];
        // additional stuff
    }
}

关于ios - Xcode 4.5 中的当前位置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13558362/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4