当前位置更新在iphone设备中不起作用

当前位置更新在iphone设备中不起作用,iphone,ios,currentlocation,Iphone,Ios,Currentlocation,正在处理需要每秒更新当前位置的应用程序 为此,我使用了CoreLocation框架 我的代码是 AppDelegate.h @interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate> { } @property (nonatomic, retain) CLLocationManager *locationManager; 并实现它的委托方法 -(void)locat

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

为此,我使用了
CoreLocation
框架

我的代码是

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>
{

}
@property (nonatomic, retain) CLLocationManager *locationManager;
并实现它的委托方法

-(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次

我需要这段代码在iPhone4和iPhone5中都能工作

我还没有找到任何解决办法

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

请帮忙


谢谢

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

更新:

看到这个答案了吗


我的印象是,它不会一直更新位置,只会更新3-5次,以便提供一些备选方案,供您选择最佳方案。尝试使用[locationManager startUpdatingLocation]重新开始职位生成和[locationManager StopUpdateLocation];在您的分解代码中停止。

他们使用CLLocationManagerDelegate协议引用来实现此…)看这个链接,伙计。。请参见第二个答案。您还想在后台模式下获取位置吗???@ParasJoshi:很抱歉延迟回复。我正在使用CLLocationManagerDelegate协议????我想我正在使用这个。不,我不想在后台使用它。感谢您的快速回复。我正在使用[locationManager startUpdatingLocation],但不工作。请在调用startUpdatingLocation之前尝试停止它;)或者,您可以开始更新,创建后台线程,并根据需要的时间(1s)检查[location\u manager location]属性,忽略发送给代理的任何更新。您还可以保存旧位置,以便以后知道其是否已更改。如果更改-发送消息选择器,无论什么到主线程(重要)更新您的UI。我可以有任何演示吗?b'coz不知道该做什么…这很简单,应该运行,但会产生问题…抱歉,没有时间创建演示。在此网站上搜索一些示例。我记得我见过一些。
-(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]);

}