2

处理需要每秒更新当前位置的应用程序。

为此,我正在使用CoreLocation框架。

我的代码就像

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>
{

}
@property (nonatomic, retain) CLLocationManager *locationManager;

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
    [locationManager startMonitoringSignificantLocationChanges];
    [locationManager retain];
}

并实现它的委托方法

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
   CLLocation *locB = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];

    NSLog(@"New location is >>>>>> %@",locB);

}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@">>>>>>>>>> error :%@", [error description]);

}

我的问题是,当我将此代码运行到模拟器中时,它工作正常并且每秒更新一次位置,但是当我将它运行到设备中时,它只显示位置更新 3 或 4 次。

我需要这段代码在 iphone 4 和 iphone 5 中都可以使用。

我还没有找到任何解决方案。

实际上我需要计算距离或计算步数..为此我也可以加速度计,但它并没有给出完美的解决方案。

请帮忙...

谢谢 !

4

2 回答 2

1

我的印象是它不会一直更新位置,只会更新 3-5 次,以便提供一些替代方案,您可以使用这些替代方案来找到最好的。尝试使用 [locationManager startUpdatingLocation] 重新开始位置生成和 [locationManager stopUpdatingLocation];在您的拆解代码中停下来。

于 2013-01-01T13:36:43.220 回答
0

您不能在后台运行应用程序。您必须将其注册为在后台运行的应用程序。请参阅此链接中的实现长时间运行的后台任务部分

管理您的应用程序流程

更新:

看到这个答案

iPhone SDK:使用 GPS 跟踪用户位置

于 2013-01-01T13:18:54.453 回答