Xcode 4.5中的当前位置错误 [英] Current Location Bug in Xcode 4.5

查看:165
本文介绍了Xcode 4.5中的当前位置错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

苹果在Xcode 4.5中引入了苹果新地图。我的应用比较重,需要地图服务。而且我注意到我的应用程序显示错误的当前位置,直到您删除该应用程序并重新打开它显示正确的当前位置(有时它不)。只要提到我连接到4G,当它显示错误的当前位置。有没有办法解决这个bug,因为我的应用程序需要正确的位置。提前致谢。如果你可以提供代码,这将有助于很多... 编辑: Apple Map App中的相同问题



我的代码: / 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.horizo​​ntalAccuracy< 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.horizo​​ntalAccuracy< 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆