首次加载地图时,无法将UIMapView设置为“缩放"/setRegion: [英] Cannot get UIMapView to "zoom"/setRegion: when the map is loaded for the first time

查看:120
本文介绍了首次加载地图时,无法将UIMapView设置为“缩放"/setRegion:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIViewController,它会加载带有一堆引脚的地图.使销钉出现没有问题,但是当用户第一次到达mapView时,我似乎无法获得MKMapView来执行setRegion.我尝试将代码放入所有可行的方法中,例如viewDidLoadviewWillLoadmapViewWillStartLoadingMapmapViewDidFinishLoadingMap等.

I have a UIViewController that loads a map with a bunch of pins already on it. There's no issues getting the pins to appear, but I can't seem to get MKMapView to execute setRegion when the user first reaches the mapView. I've tried putting the code in every possible method that could work, viewDidLoad, viewWillLoad, mapViewWillStartLoadingMap, mapViewDidFinishLoadingMap, etc., to no avail.

但是,如果我返回上一个viewController,然后再次进入包含mapView的viewContoller,它将缩放"到指定区域.

And yet, it WILL "zoom" to the designated region if I return to the previous viewController and then go into the viewContoller containing the mapView again.

是否存在我不知道的超级秘密方法?调用[mapView setRegion:(...) animated:YES];是否足以使地图缩放"/更新以显示所选区域?

Is there a super secret way of doing this that I'm not aware of? Shouldn't calling [mapView setRegion:(...) animated:YES]; be enough to get the map to "zoom"/update to show the chosen region??

此外,如果有人知道如何获取图钉(我正在使用MKPointAnnotation作为我的图钉)来动画化拖放以及使地图动画化放大,(尽管我都将其设置为动画....),我也希望得到该信息.

Also, if anybody knows how to get the pins (I'm using MKPointAnnotation for mine) to animate dropping in as well as getting the map to animate zooming in, (neither are doing it, although I set both to animate....), I'd appreciate that info as well.

谢谢!

代码:

@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];
}

这对我有用.

这篇关于首次加载地图时,无法将UIMapView设置为“缩放"/setRegion:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆