2011-06-29 40 views
0

嗨我有一個需求,我需要在應用程序在後臺運行時將用戶的位置更新到服務器。我已經使用下面的代碼在車上進行測試,並且發現 - (void)locationManager:(CLLocationManager *)管理器didUpdateToLocation:被觸發時,lat和long中報告的差異很小。座標距離那時我真的在那裏。我知道didupdatelocation是否使用單元格塔的讀數,因此不是非常準確。我想知道是否有無論如何我可以啓動[locationManager startUpdatingLocation] - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:;然後獲得更精確的讀數。如何獲取locationManager startMonitoringSignificantLocationChanges準確讀數

請任何幫助或指示將是偉大的,高度讚賞。

#import "LocationsAppDelegate.h" 
#import "RootViewController.h" 


@implementation LocationsAppDelegate 

@synthesize window; 
@synthesize navigationController; 

-(void)initLocationManager { 
    if (locationManager == nil) { 
     locationManager = [[CLLocationManager alloc] init]; 
     locationManager.delegate = self; 
     locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move 
     locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; // 100 m 
     // [locationManager startUpdatingLocation]; 
     [locationManager startMonitoringSignificantLocationChanges]; 
    } 
} 

- (void)saveCurrentData:(NSString *)newData { 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSMutableArray *savedData = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"kLocationData"]]; 
    [savedData addObject:newData]; 
    [defaults setObject:savedData forKey:@"kLocationData"]; 
    [savedData release]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 

    NSDate* eventDate = newLocation.timestamp; 

    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; 

    //check if the data is less than 15 sec ago 
    if (abs(howRecent) > 15.0) 
    { 
     NSLog(@"old data latitude %+.6f, longitude %+.6f\n", 
     newLocation.coordinate.latitude,newLocation.coordinate.longitude); 

    }else{ 

     [locationManager startUpdatingLocation]; 

     NSDateFormatter * formatter = [[[NSDateFormatter alloc] init] autorelease]; 
     [formatter setTimeStyle:NSDateFormatterMediumStyle]; 
     NSString *locationData = [NSString stringWithFormat:@"%.6f, %.6f, %@",newLocation.coordinate.latitude, newLocation.coordinate.longitude,[formatter stringFromDate:newLocation.timestamp]]; 

     [self saveCurrentData:locationData]; 

     [locationManager stopUpdatingLocation]; 
    }  

} 

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { 
    NSString *errorData = [NSString stringWithFormat:@"%@",[error localizedDescription]]; 
    NSLog(@"%@", errorData); 
} 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
    if (![CLLocationManager significantLocationChangeMonitoringAvailable]) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Your device won't support the significant location change." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
     return YES; 
    } 
    [self initLocationManager]; 
    [self.window addSubview:navigationController.view]; 
    [self.window makeKeyAndVisible]; 

    return YES; 
} 

- (void)dealloc { 
    [locationManager release]; 
    [navigationController release]; 
    [window release]; 
    [super dealloc]; 
} 

@end 

回答

0

startMonitoringSignificantLocationChanges的行爲不會受到distanceFilterdesiredAccuracy性質。

所以,如果你想獲取更多accurated結果,稱startUpdatingLocation,並通過與distanceFilterdesiredAccuracy性質(kCLLocationAccuracyBestForNavigationkCLLocationAccuracyBest)玩是最accurated,但是比較耗電,所以和他們一起玩。

我完全不明白你的代碼的行爲。爲什麼你打電話startMonitoringSignificantLocationChanges而不是startUpdatingLocation,但你在委託方法中調用它?

+0

感謝Yorxxx您的答覆。我希望應用程序在後臺運行並在顯着距離更改後發佈數據。如何在後臺運行應用程序並在用戶行駛了很長距離時喚醒應用程序。再次感謝你的幫助。 – snowflakes74

相關問題