2013-01-01 46 views
2

正在處理需要每秒更新當前位置的應用程序。當前位置更新無法在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,我也可以加速度計,但它沒有給出完美的解決方案。

請幫忙...

謝謝!

+0

嘿使用CLLocationManagerDelegate協議參考爲此.. :) –

+0

請參閱此鏈接夥計.. http://stackoverflow.com/questions/6516967/how-to-find-your-current-location-with-corelocation請參閱第二個答案從.. –

+0

你想獲得的位置在後臺模式也?? –

回答

1

我的印象是,它不會將所有更新位置時間,只有3-5次,才能提供一些替代品,你可以用它來找到最好的。嘗試使用[locationManager startUpdatingLocation]重新開始職位生成和[locationManager stopUpdatingLocation];停止在您的拆卸代碼。

+0

感謝您的快速回復..我使用[locationManager startUpdatingLocation]但不工作。 –

+0

嘗試停止它,然後再調用startUpdatingLocation;) –

+0

我需要位置更新每秒或2秒 –

相關問題