当mapview打开iOS时,动画缩放到位置/地图地标

时间:2012-12-06 13:56:50

标签: ios6 mkmapview zoom mkcoordinateregion

我已经在其他应用程序上看到过6个星巴克,当我的mapview打开时,我希望它显示整个英国/英国岛屿的区域,然后我希望它放大到我指定的位置区域我有点。

更新代码:

- (void)viewDidLoad
{

[super viewDidLoad];
// Do any additional setup after loading the view.
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 54.5;
region.center.longitude = -3.5;
region.span.longitudeDelta = 10.0f;
region.span.latitudeDelta = 10.0f;
[mapView setRegion:region animated:NO];

[self performSelector:@selector(zoomInToMyLocation)
           withObject:nil
           afterDelay:2]; //will zoom in after 1.5 seconds
}

-(void)zoomInToMyLocation
{
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 51.502729 ;
region.center.longitude = -0.071948;
region.span.longitudeDelta = 0.19f;
region.span.latitudeDelta = 0.19f;
[mapView setRegion:region animated:YES];

[mapView setDelegate:self];

[self performSelector:@selector(selectAnnotation)
           withObject:nil
           afterDelay:0.5]; //will zoom in after 0.5 seconds

}

-(void)selectAnnotation
{

DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = @"Design Museum";
ann.subtitle = @"Camberwell, London";
ann.coordinate = region.center;
[mapView addAnnotation:ann];

}

不知道它是否正确,因为错误是这一行

ann.coordinate = region.center;

2 个答案:

答案 0 :(得分:11)

如果您想首先显示一个区域然后放大,则必须发出两个或多个setRegion来电,因为setRegion本身不允许您控制开始< / em>区域或动画的速度。

viewDidLoad中,设置初始区域span,以便显示整个英国(尝试10.0而不是0.15的增量)。您还可以将animated设置为NO作为初始区域。

然后在viewDidLoad结束之前,安排在几秒钟后执行放大:

- (void)viewDidLoad
{
    ...

    [self performSelector:@selector(zoomInToMyLocation) 
               withObject:nil 
               afterDelay:5]; //will zoom in after 5 seconds
}

zoomInToMyLocation方法可能如下所示:

-(void)zoomInToMyLocation
{
    MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
    region.center.latitude = 51.502729 ;
    region.center.longitude = -0.071948;
    region.span.longitudeDelta = 0.15f;
    region.span.latitudeDelta = 0.15f;
    [mapView setRegion:region animated:YES];
}


使用performSelector时可能需要注意的一件事是,如果视图在计划运行之前关闭或取消分配,则取消挂起的呼叫。例如,如果用户在加载后两秒钟关闭视图。三秒钟后,调度的方法可能仍然会被调用,但会因视图消失而崩溃。为避免这种情况,请取消viewWillDisappear:或适当的任何待处理的执行:

[NSObject cancelPreviousPerformRequestsWithTarget:self];

答案 1 :(得分:0)

MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = Your Latitude ;
region.center.longitude = Your Longitude;
region.span.longitudeDelta =  0.01f;
region.span.latitudeDelta =  0.01f;
[map setRegion:region animated:YES];
[map addAnnotation:ann];