0

我不明白为什么没有向代表发送消息 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
4

1 回答 1

0

我认为您混淆了 和 的位置CLLocationManager服务MapViewmapView:didUpdateUserLocation:是 的委托方法MKMapView。如果你只需要这个方法来更新用户的位置,你不必实例化一个CLLocationManager. 相反,您只需说,就像您所做[worldView setShowsUserLocation:YES];
的那样,但是当然,您实现的对象mapView:didUpdateUserLocation:必须是您的对象的委托MKMapView,这里是 的worldView,而不是CLLocationManager对象的!
所以我的建议是将你的对象设置为你的TrackViewController对象的委托MKMapView,即 of worldView,并删除与CLLocationManager.

于 2013-07-28T18:58:54.650 回答