Cannot get UIMapView to “zoom”/setRegion: when the map is loaded for the first time

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-14 01:03:37

问题


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.

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.

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??

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.

Thanks!

Code:

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

回答1:


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

This works for me.



来源:https://stackoverflow.com/questions/13017461/cannot-get-uimapview-to-zoom-setregion-when-the-map-is-loaded-for-the-first-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!