Location services, how to update latitude, Longitude after some time (30 sec)?

。_饼干妹妹 提交于 2019-12-24 21:15:24

问题


I am getting latitude, Longitude, Altitude, Accuracy, Speed from Location services.

I want to update all this information every 30 sec. For this purpose I've used a NSTimer:

[NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(locationUpdate:) userInfo:nil repeats:YES];

which I've put in the viewDidLoad method. It shows an output first time, but the second time, when the timer invokes this, it shows this error:

2011-08-02 13:17:00.141 CoreLocationAssign[1120:207] This is location   <__NSCFTimer: 0x4b200a0>
2011-08-02 13:17:00.142 CoreLocationAssign[1120:207] -[__NSCFTimer coordinate]: unrecognized selector sent to instance 0x4b200a0
2011-08-02 13:17:00.144 CoreLocationAssign[1120:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFTimer coordinate]: unrecognized selector sent to instance 0x4b200a0'
*** Call stack at first throw:

The code is below:

MyCLController.h

@implementation MyCLController
@synthesize locationManager;
@synthesize delegate;

- (id) init{
    if (self!=nil) {
        self.locationManager  = [[[CLLocationManager alloc] init] autorelease];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = kCLDistanceFilterNone;
        [self.locationManager startUpdatingLocation];
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    //NSLog(@"Location: %@", [newLocation description]);
    [self.delegate locationUpdate:newLocation];
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
//  NSLog(@"Error: %@", [error description]);
    [self.delegate locationError:error];
}

- (void)dealloc {
    [self.locationManager release];
    [super dealloc];
}

CoreLoactionController.h
- (void)viewDidLoad {
    locationController = [[MyCLController alloc] init];
    locationController.delegate = self;
    [locationController.locationManager startUpdatingLocation];
    [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(locationUpdate:) userInfo:nil repeats:YES];
    [super viewDidLoad];
}


- (void)locationUpdate:(CLLocation *)location {
    locationLable.text = [location description];
    CLLocationCoordinate2D coordinate = [location coordinate];
    NSLog(@"This is location   %@",[location description]);
    NSLog(@"Lattitude          =   %@",[NSString stringWithFormat:@"%f", coordinate.latitude]);
    NSLog(@"Langitude          =   %@",[NSString stringWithFormat:@"%f", coordinate.longitude]);
    NSLog(@"Altitude           =   %@",[NSString stringWithFormat:@"%gm", location.altitude]);
    NSLog(@"horizontalAccuracy =   %@",[NSString stringWithFormat:@"%gm", location.horizontalAccuracy]);
    NSLog(@"verticalAccuracy   =   %@",[NSString stringWithFormat:@"%gm", location.verticalAccuracy]);
    NSLog(@"Speed   =   %@",[NSString stringWithFormat:@"%gm", location.speed]);

}

How ccan I solve this problem? I want to update the location every 30 sec.


回答1:


In fact here :

- (void)locationUpdate:(CLLocation *)location

You're not receiving a CLLocation * but a NSTimer. You should use the userInfo to transmit an object:

[NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(locationUpdate:) userInfo:location repeats:YES];

and

-(void)locationUpdate:(NSTimer*) timer{
    //probably
    CLLocation *location = (CLLocation*)[timer userInfo];
    //...
}



回答2:


There are better solutions than using a timer. CoreLocation has built in features for deferred updates and a timer combined with normal CoreLocation will destroy phone battery. Try allowDeferredLocationUpdatesUntilDistanceTraveled:timeout:

https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/occ/instm/CLLocationManager/allowDeferredLocationUpdatesUntilTraveled:timeout:




回答3:


Timer-based location updates are horrible battery eaters.

CoreLocation can tell you when the location gets changed, no need for polling. 30 seconds is much too slow if I'm even walking, and it needlessly destroys the battery if my phone is lying near my bed unmoved for many hours.



来源:https://stackoverflow.com/questions/6883089/location-services-how-to-update-latitude-longitude-after-some-time-30-sec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!