正在處理需要每秒更新當前位置的應用程序。當前位置更新無法在iphone中工作設備
因爲我使用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
工作,我還沒有找到任何解決方案。
其實我需要計算距離或計數steps..for,我也可以加速度計,但它沒有給出完美的解決方案。
請幫忙...
謝謝!
嘿使用CLLocationManagerDelegate協議參考爲此.. :) –
請參閱此鏈接夥計.. http://stackoverflow.com/questions/6516967/how-to-find-your-current-location-with-corelocation請參閱第二個答案從.. –
你想獲得的位置在後臺模式也?? –