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

时间:2012-10-22 18:28:33

标签: iphone ios mkmapview mapkit

我有一个UIViewController,用于加载已经有一堆引脚的地图。让引脚显示没有问题,但是当用户第一次到达mapView时,我似乎无法让MKMapView执行setRegion。我已经尝试将代码放在可行的每种可能方法中,viewDidLoadviewWillLoadmapViewWillStartLoadingMapmapViewDidFinishLoadingMap等等,但无济于事。

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

我是否有超级秘密的做法,我不知道?不应该调用[mapView setRegion:(...) animated:YES];足以让地图“缩放”/更新以显示所选区域吗?

另外,如果有人知道如何获取引脚(我正在使用MKPointAnnotation为我的动画)动画放入以及让地图进行动画放大,(虽然我设置了既动画....),我也很欣赏这些信息。

谢谢!

代码:

@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 个答案:

答案 0 :(得分:0)

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

这适合我。