在Xcode 4.5苹果推出苹果新地图。 我的应用程序heavliy需要地图服务。 我在我的应用程序已经注意到它表明,直到删除应用程序,并重新打开错误的当前位置它显示正确的当前位置(有时事实并非如此)。 只提我连接到4G时,它显示了错误的当前位置。 有没有办法解决这个问题,因为我的应用程序需要大量的正确的位置。 提前致谢。 如果你能提供代码,将有很大的帮助... 编辑:同样的问题,在苹果地图应用程序中
我的代码:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
{
}
if (!oldLocation)
totalDistance = 0.0;
else
totalDistance += [newLocation distanceFromLocation:oldLocation];
}
从苹果文档似乎仍然在iOS6的工作旧的方法(在我的活跃APP没有注意到这一点(它通过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
}
从讨论UPDATE:计算总的和当前距离可以这样做(不包括一些小的东西):
// 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
}
}
我已经和Xcode 4.4或以上版本发现了同样的问题。 只有出现问题(随机,或者看起来是这样)的Xcode中:如果你上传的应用程序在App Store,这是不是一个问题了。 在此期间,请提交错误报告。
你应该检查时间戳..如果我正确地了解自己的应用逻辑,你可以这样做 -
- (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
}
}