委托方法未被发送

时间:2013-07-28 18:47:20

标签: ios objective-c

我不明白为什么委托没有发送消息mapView:didUpdateUserLocation:

在viewDidLoad中,我要求mapView使用setShowUserLocation启动更新:YES。但就像它永远不会实现这一点。

有什么建议吗?

#import "TrackViewController.h"
#import "MainWindowViewController.h"



@class MainWindowViewController;
@implementation TrackViewController

- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    NSLog(@"Init trackcontriller");
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if(self){

    locationManager = [[CLLocationManager alloc]init];

    [locationManager setDelegate:self];

    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];


    }
    return self;
}

-(IBAction) back:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void) findLocation;
{
    NSLog(@"findlocation");
    [locationManager startUpdatingLocation];

}

-(void) foundLocation:(CLLocation *)loc
{
    NSLog(@"Foundlocation");
    CLLocationCoordinate2D coord = [loc coordinate];

    MKCoordinateRegion region =  MKCoordinateRegionMakeWithDistance(coord, 100, 100);
    [worldView setRegion:region animated:YES];


    [locationManager stopUpdatingLocation];
}

-(void)viewDidLoad
{
    NSLog(@"viewDidLoad");
    [worldView setShowsUserLocation:YES];
    [worldView setMapType:MKMapTypeHybrid];

}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSLog(@"mapview didupdateUserlocation");
    CLLocationCoordinate2D loc = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 200, 200);
    [worldView setRegion:region animated:YES];
}

-(void)dealloc 
{

     [locationManager setDelegate:nil];
}

- (void) locationManager:(CLLocationManager *) manager
 didUpdateToLocation:(CLLocation *)newLocation
        fromLocation:(CLLocation *)oldLocation
// Deprecated i know
{
     NSLog(@"Locationanager didupdate tolocation from location");


NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];

if(t<-180){
    return;
}

    [self foundLocation:newLocation];
}

-(void)locationManager:(CLLocationManager *)manager
  didFailWithError:(NSError *)error
{
    NSLog(@"Could not find location: %@", error);
}

@end

1 个答案:

答案 0 :(得分:0)

我认为您混淆了CLLocationManagerMapView的位置服务。 mapView:didUpdateUserLocation:MKMapView的委托方法。如果您只需要此方法来更新用户的位置,则不必实例化CLLocationManager。相反,你只是像你一样说[worldView setShowsUserLocation:YES];
但是,当然,实现mapView:didUpdateUserLocation:的对象必须是MKMapView对象的委托,此处为worldView,而不是CLLocationManager对象! 因此,我的建议是将您的TrackViewController对象设置为MKMapView对象的代理,即worldView,并删除与CLLocationManager相关的代码。