Маршрут MKOverlay работает в коде Apple Breadcrumb iOS, но не в моем приложении

Я работаю над приложением iOS и хочу включить функцию маршрута Breadcrumb iOS Mapkit, предоставляемую Apple, в качестве одной из функций. Я создал UIViewController в раскадровке (как вкладку из контроллера панели вкладок) и вставил в него MKMapView. Я также подключил его к розетке в ThirdViewController, как показано ниже. Классы показаны ниже. У меня есть классы CrumbPath и CrumbPathView точно так же, как в примере Breadcrumb на http://developer.apple.com/library/ios/#samplecode/Breadcrumb/Introduction/Intro.html

Даже с тем же кодом маршрут mkoverlay не отображается в моем приложении. Я пропустил что-то важное здесь. У меня нет опыта программирования под iOS, и я мог упустить что-то основное.

ThirdViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

#import "CrumbPath.h"
#import "CrumbPathView.h"

@interface ThirdViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
{

@private
    MKMapView *map;

    CrumbPath *crumbs;
    CrumbPathView *crumbView;

    CLLocationManager *locationManager;

}

@property (nonatomic, retain) IBOutlet MKMapView *map;
@property (nonatomic, retain) CLLocationManager *locationManager;
@end

ThirdViewController.m

#import "ThirdViewController.h"

@interface ThirdViewController ()
@end

@implementation ThirdViewController

@synthesize locationManager, map;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.

    self.wantsFullScreenLayout = YES;

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [self.locationManager startUpdatingLocation];

    [self.view addSubview:self.map];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.

    self.map = nil;
    self.locationManager.delegate = nil;
    self.locationManager = nil;
}

-(void) dealloc
{

}

- (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark MapKit

- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if(newLocation)
    {
        if((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) && (oldLocation.coordinate.longitude != newLocation.coordinate.longitude))
        {
            if(!crumbs)
            {
                crumbs = [[CrumbPath alloc] initWithCenterCoordinate:newLocation.coordinate];
                [map addOverlay:crumbs];

                MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 2000, 2000);
                [map setRegion:region animated:YES];
            }
            else
            {
                MKMapRect updateRect = [crumbs addCoordinate:newLocation.coordinate];

                if(!MKMapRectIsNull(updateRect))
                {
                    MKZoomScale currentZoomScale = (CGFloat)(map.bounds.size.width/map.visibleMapRect.size.width);

                    CGFloat lineWidth = MKRoadWidthAtZoomScale(currentZoomScale);
                    updateRect = MKMapRectInset(updateRect, -lineWidth, -lineWidth);

                    [crumbView setNeedsDisplayInMapRect:updateRect];
                }
            }
        }
    }
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
    if(!crumbView)
    {
        crumbView = [[CrumbPathView alloc] initWithOverlay:overlay];
    }
    return crumbView;
}
@end

person Sap    schedule 14.08.2012    source источник
comment
Вы установили свой ThirdViewController в качестве делегата для своего MKMapView в своей раскадровке?   -  person jonkroll    schedule 14.08.2012
comment
Упс. Я определенно должен улучшить проверку всех этих вещей. Я подключил его сейчас, и наложение отображается. Большое спасибо!   -  person Sap    schedule 14.08.2012
comment
Как я могу закрыть этот вопрос и/или упомянуть, что на него дан ответ?   -  person Sap    schedule 14.08.2012
comment
Я добавлю свой комментарий в качестве ответа, и тогда вы сможете пометить его как принятый ответ.   -  person jonkroll    schedule 14.08.2012


Ответы (1)


В вашей раскадровке ваш ThirdViewController не был назначен делегатом вашего MKMapView, поэтому mapView:viewForOverlay: никогда не вызывался. Установка свойства делегата устраняет проблему.

person jonkroll    schedule 14.08.2012