【发布时间】:2012-12-16 03:25:05
【问题描述】:
正在处理需要每秒更新当前位置的应用程序。
为此,我正在使用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 上都能工作。
我还没有找到任何解决方案。
实际上我需要计算距离或计算步数..为此我也可以使用加速度计,但它并没有给出完美的解决方案。
请帮忙...
谢谢!
【问题讨论】:
-
嘿,为此使用 CLLocationManagerDelegate 协议参考 .. :)
-
看到这个链接老兄.. stackoverflow.com/questions/6516967/… 看到第二个答案..
-
你也想在后台模式下获取位置??
-
@ParasJoshi:抱歉回复晚了..我正在使用 CLLocationManagerDelegate 协议????我想我正在使用那个。
-
不,我不想在后台使用它。
标签: iphone ios currentlocation