简体   繁体   English

MKMapView不会缩放或设置动画

[英]MKMapView won't zoom or animate

Trying to learn my way around MKMapView, but I've followed several tutorials and I can't seem to get it to zoom into my defined location, instead it just loads to a whole map view centered around the Atlantic ocean. 尝试学习有关MKMapView的方法,但是我遵循了一些教程,但似乎无法将其放大到定义的位置,而是将其加载到以大西洋为中心的整个地图视图中。 I'm guessing something has changed in iOS6 that means the tutorials I am using no longer work? 我猜测iOS6中发生了一些变化,这意味着我正在使用的教程不再起作用? (The tutorials are iOS3-iOS5). (这些教程是iOS3-iOS5)。

I've made a ViewController in Interface Builder, given it the custom class of MapViewController. 我已经在Interface Builder中制作了一个ViewController,并为其指定了MapViewController的自定义类。 I then dragged on MKMapView and using the Assistant Editor put this into MapViewController.h 然后,我在MKMapView上拖动并使用助手编辑器将其放入MapViewController.h

MapViewController.h MapViewController.h

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

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

@end

MapViewController.m 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

The code for the region looks correct. 该区域的代码看起来正确。 Are you sure you have linked up the myMapView outlet in interface builder? 您确定已在界面生成器中链接了myMapView出口吗?

在此处输入图片说明

If you click and hold on the "File's Owner" object on the left, then hold Ctrl and drag from the File's Owner to the map view in the interface, when you let go of the mouse button you should get the option to set the outlet to myMapView . 如果单击并按住左侧的“文件所有者”对象,则按住Ctrl并将其从文件所有者拖到界面中的地图视图中,放开鼠标按钮时,您应该可以选择设置出口到myMapView You can then see the link in the Connections inspector as shown on the right. 然后,您可以在“连接”检查器中看到该链接,如右图所示。

EDIT: 编辑:

OK, so it appears that, as you are using Autolayout, the map view's frame has not been set by the time viewDidLoad gets called. 好的,看来,当您使用自动布局时,在调用viewDidLoad时尚未设置地图视图的框架。 I suggest you move it to - (void)viewWillLayoutSubviews . 我建议您将其移动到- (void)viewWillLayoutSubviews For example: 例如:

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

-(void)setLocation {
    // Set region etc...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM