开发者

iphone: CLLocation manager delegate method is not called on click of a button

one more bad day god knows whats the problem with this but delegate methods for CLLocation Mangaer are not called once I click a refresh button, on click of this button I ask location manager to update location

-(void) viewWillAppear:(BOOL)animated{


locationManager=[[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.delegate = self;
[locationManager startUpdatingLocation];

}

 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{

NSLog(@"failed");

}

  - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation开发者_开发百科 fromLocation:(CLLocation *)oldLocation{


NSLog(@"current lat= %f and long=%f ", newLocation.coordinate.latitude, newLocation.coordinate.longitude);

NSDate *eventDate = newLocation.timestamp; 
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];

if (abs(howRecent) < 10) {

    [lat setText:[ NSString stringWithFormat:@"%f", newLocation.coordinate.latitude]];
    [lon setText:[ NSString stringWithFormat:@"%f", newLocation.coordinate.longitude]];

}

else {
    [lat setText:@"Not changed"];
    [lon setText:@"Not changed"];
}

}

  -(IBAction) refrechLoc{

NSLog(@"updating location");
[locationManager startUpdatingLocation];

} Please help me resolve this issue.

Thanks in advance


It seems that it is beginning updating on viewWillAppear, so it won't update the location until it is not stopped by stopUpdatingLocation. If you want to restart updating you should call stopUpdatingLocation before startUpdatingLocation:

-(IBAction) refrechLoc{
    NSLog(@"updating location");
    [locationManager stopUpdatingLocation];
    [locationManager startUpdatingLocation];
}

imho, there is no need in location updating restart in general


You've misunderstood how CLLocationManager works. The delegate is called when CLLocationManager has a new location for you. [locationManager startUpdatingLocation]; merely tells the location manager to start updating the location, so there's no need to call that twice.

EDIT: For clarity: The delegate is called when the location is updated, i.e., when the device has moved.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜