无法让 UIMapView 进行“缩放”/setRegion:第一次加载地图时

2024-02-01

我有一个UIViewController加载一张已经有一堆图钉的地图。让图钉出现没有问题,但我似乎无法得到MKMapView当用户第一次到达mapView时执行setRegion。我尝试将代码放入每一种可能有效的方法中,viewDidLoad, viewWillLoad, mapViewWillStartLoadingMap, mapViewDidFinishLoadingMap等,均无济于事。

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

有没有我不知道的超级秘密方法?不应该打电话[mapView setRegion:(...) animated:YES];足以让地图“缩放”/更新以显示所选区域?

另外,如果有人知道如何获取引脚(我正在使用MKPointAnnotation对于我的)动画下降以及让地图动画放大,(都没有这样做,虽然我将两者都设置为动画......),我也很欣赏这些信息。

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

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

这对我有用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法让 UIMapView 进行“缩放”/setRegion:第一次加载地图时 的相关文章

随机推荐