0

嗨,我有一个要求,我需要在应用程序在后台运行时将用户的位置更新到服务器。我已经使用下面的代码在汽车中对其进行了测试,并且发现当 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation: 被触发时报告的经纬度几乎没有差异。坐标距离那一刻我实际上在哪里。

我知道 didupdatelocation 使用手机信号塔读数,因此不是很准确。我想知道当 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:; 时我是否可以启动 [locationManager startUpdatingLocation] 然后得到更准确的读数。

请任何帮助或指示将不胜感激。

#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
4

1 回答 1

0

的行为startMonitoringSignificantLocationChanges不受distanceFilterdesiredAccuracy属性的影响。

因此,如果您想检索更准确的结果,请调用startUpdatingLocation,并通过使用distanceFilteranddesiredAccuracy属性 ( kCLLocationAccuracyBestForNavigation, kCLLocationAccuracyBest) 是最准确的,但会消耗更多的电量,因此请使用它们。

我不完全了解您的代码的行为。为什么你调用startMonitoringSignificantLocationChanges而不是startUpdatingLocation调用它,但你在委托方法中调用它?

于 2011-06-29T13:10:56.577 回答