0

试图了解 MKMapView 的方法,但我已经学习了几个教程,但我似乎无法让它放大到我定义的位置,而是加载到以大西洋为中心的整个地图视图。我猜 iOS6 发生了一些变化,这意味着我正在使用的教程不再有效?(教程是iOS3-iOS5)。

我在 Interface Builder 中制作了一个 ViewController,给它一个 MapViewController 的自定义类。然后我拖动 MKMapView 并使用助手编辑器将其放入 MapViewController.h

MapViewController.h

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

@interface MapViewController : UIViewController
@property (weak, nonatomic) IBOutlet MKMapView *myMapView;

@end

MapViewController.m

#import "MapViewController.h"

#define CLS_LATITUDE 54.85477
#define CLS_LONGITUDE -1.57371
#define SPAN 0.10f;

@interface MapViewController ()

@end

@implementation MapViewController
@synthesize myMapView;

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

 - (void)viewDidLoad
{
    [super viewDidLoad];
    MKCoordinateRegion myRegion;
    CLLocationCoordinate2D center;
    center.latitude = CLS_LATITUDE;
    center.longitude = CLS_LONGITUDE;
    MKCoordinateSpan mySpan;
    mySpan.latitudeDelta = SPAN;
    mySpan.longitudeDelta = SPAN;
    myRegion.center = center;
    myRegion.span = mySpan;
    [myMapView setRegion:myRegion animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 @end
4

1 回答 1

1

该地区的代码看起来是正确的。您确定已在界面生成器中链接了 myMapView 插座吗?

在此处输入图像描述

如果您单击并按住左侧的“文件所有者”对象,然后按住Ctrl并从文件所有者拖动到界面中的地图视图,当您松开鼠标按钮时,您应该可以选择设置插座到myMapView. 然后,您可以在 Connections 检查器中看到该链接,如右侧所示。

编辑:

viewDidLoad好的,所以看起来,当您使用自动布局时,地图视图的框架在调用时尚未设置。我建议你把它移到- (void)viewWillLayoutSubviews. 例如:

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [self setLocation];
}

-(void)setLocation {
    // Set region etc...
}
于 2012-11-14T20:22:58.247 回答