0

我正在开发一个 iOS 应用程序,并希望将苹果提供的 Breadcrumb iOS Mapkit 路由功能作为功能之一。我在情节提要中创建了一个 UIViewController(作为选项卡栏控制器的选项卡)并在其中插入了一个 MKMapView。我还将它连接到下图所示的 ThirdViewController 中的插座。类如下所示。我的 CrumbPath 和 CrumbPathView 类与http://developer.apple.com/library/ios/#samplecode/Breadcrumb/Introduction/Intro.html的 Breadcrumb 示例完全相同

即使使用相同的代码,mkoverlay 路由也不会显示在我的应用程序中。我在这里错过了一些重要的事情吗?我在 iOS 编程方面没有经验,可能错过了一些基本的东西。

第三视图控制器.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

第三视图控制器.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
4

1 回答 1

1

您的故事板中没有您的ThirdViewController集合作为您的代表MKMapView,因此mapView:viewForOverlay:从未被调用。设置委托属性可以解决问题。

于 2012-08-14T16:59:22.023 回答