iphone - 无法将 UIMapView 获取到 "zoom"/setRegion : when the map is loaded for the first time

标签 iphone ios mkmapview mapkit

我有一个 UIViewController 可以加载一张 map ,上面已经有一堆图钉。让图钉出现没有问题,但我似乎无法让 MKMapView 在用户首次到达 mapView 时执行 setRegion。我已尝试将代码放入所有可行的方法中,viewDidLoadviewWillLoadmapViewWillStartLoadingMapmapViewDidFinishLoadingMap等,无济于事。

然而,如果我返回到前一个 viewController,然后再次进入包含 mapView 的 viewContoller,它将“缩放”到指定区域。

是否有一种我不知道的 super secret 方法?调用 [mapView setRegion:(...) animated:YES]; 是否足以使 map “缩放”/更新以显示所选区域?

此外,如果有人知道如何获取图钉(我正在使用 MKPointAnnotation)来制作插入动画以及制作 map 动画放大,(两者都没有这样做,尽管我将两者都设置为动画......),但我也很感激该信息。

谢谢!

代码:

@implementation FindOnMapMapView
@synthesize mapView;
@synthesize mapViewTabBarItem;
@synthesize results;
@synthesize reverseGeocoder;

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

        MKCoordinateRegion mapRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake([defaults doubleForKey:@"latitude"], [defaults doubleForKey:@"longitude"]), MKCoordinateSpanMake(0.744494189288569, 0.744494189288569));

        mapView.region = mapRegion;
        [mapView setRegion:mapRegion animated:YES];



NSMutableArray *annotations = [[NSMutableArray alloc] init];
        for(ThePlace *place in results)
        {
            MKPointAnnotation *addAnnotation = [[MKPointAnnotation alloc] init];

            [addAnnotation setCoordinate:CLLocationCoordinate2DMake(place.latitude, place.longitude)];

            addAnnotation.title = place.placename;
            addAnnotation.subtitle = [NSString stringWithFormat:@"Distance: %@", place.distance];

            [mapView addAnnotation:addAnnotation];

        }

        [self.mapView addAnnotations:annotations ];
    }

    - (void)viewWillAppear:(BOOL)animated
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

        MKCoordinateRegion mapRegion = MKCoordinateRegionMake(CLLocationCoordinate2DMake([defaults doubleForKey:@"latitude"], [defaults doubleForKey:@"longitude"]), MKCoordinateSpanMake(0.744494189288569, 0.744494189288569));
        NSLog(@"Region Coords are: Latitude:%f, Longitude:%f", [defaults doubleForKey:@"latitude"], [defaults doubleForKey:@"longitude"]);
        mapView.region = mapRegion;
        [mapView setRegion:mapRegion animated:YES];
    }

最佳答案

- (void) viewDidAppear:(BOOL)animated {
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(firstPoint.coordinate, 2000, 2000);
    [mapView setRegion:region animated:YES];
}

这对我有用。

关于iphone - 无法将 UIMapView 获取到 "zoom"/setRegion : when the map is loaded for the first time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13017461/

相关文章:

ios - 如何根据构建配置将命令行参数传递给 Xcode

ios - 第一次设置后我似乎无法设置 MKMapView 注释标题

iphone - MKMapView setRegion animated 不显示动画

iphone - 在 iPhone 上拖动 UIScrollView 时,OpenGL ES View 中的动画卡住

iphone - 配置限量版 iOS 应用程序

iphone - 嵌入 Segue 不向后兼容

c++ - 如何获得 iPhone 6 或 6 Plus 的确切屏幕尺寸

ios - 如何在iOS中发送带有特殊字符的JSON字符串?

iphone - 是否可以在 Objective C 中创建 CLHeading 对象?

ios - 从 MapPin 中删除 subview