Xcode 4.5中的当前位置错误 [英] Current Location Bug in Xcode 4.5
问题描述
我的代码: / strong>
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
{
}
if(!oldLocation)
totalDistance = 0.0;
else
totalDistance + = [newLocation distanceFromLocation:oldLocation];
}
苹果文档的方法似乎仍然在iOS6中运行(没有注意到这在我的活动应用程序(它通过gps跟踪用户的路线))
<$ c $ (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSTimeInterval locationAge = - [newLocation.timestamp timeIntervalSinceNow];(c) - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation
if(locationAge> 5.0)return;
if(newLocation.horizontalAccuracy< 0)return;
//在这里继续进行协调
}
更新:
可以像这样计算总计和当前距离(不包括一些小的东西):
//某处顶部
CLLocation * lastUsedLocation = nil; //最后一个位置用于计算
CLLocation * pointA = nil; //开始轨道
double totalDistance = 0; //轨道总距离
double currentDistance = 0; // startTrack点和当前位置之间的距离
...
//当您开始更新位置时:
- (void)startTracking {
lastUsedLocation = nil;
pointA = nil;
totalDistance = 0;
currentDistance = 0;
[locationManager startUpdatingLocation];
}
...
//位置更新回调
- (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 *)应用程序{
if(backgroundLocationDisabled)
{
[locationManager stopUpdatingLocation];
//附加东西
}
}
In Xcode 4.5 apple introduced apple new maps. My application heavliy requires map services. And I have noticed in my application it shows the wrong current location until you delete the app and reopen it shows the right current location (Sometimes it doesn't). Just to mention I was connected to 4G when it show the wrong current location. Is there a way to fix this bug because my application heavily needs the right location. Thanks in advance. If you could provide code it would help a lot...Edit: Same Issue in Apple Map App
My Code:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
{
}
if (!oldLocation)
totalDistance = 0.0;
else
totalDistance += [newLocation distanceFromLocation:oldLocation];
}
The old approach from apple docs seems still working in iOS6 (didn't notice this in my active app (it tracks user's route via 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 from the discussion: Calculating total and current distance could be done like this (excluding some minor stuff):
// 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];
}
If you need the option to turn off background location on purpose you disable it manually like:
- (void)applicationDidEnterBackground:(UIApplication *)application {
if(backgroundLocationDisabled)
{
[locationManager stopUpdatingLocation];
// additional stuff
}
}
这篇关于Xcode 4.5中的当前位置错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!