4

我在其他应用程序(例如 ios 6 星巴克)上看到它,当我的地图视图打开时,我希望它显示整个英国/不列颠群岛的区域,然后我希望它放大到我指定的位置区域点我有.

更新代码:

- (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;
4

2 回答 2

11

如果你想从显示一个区域开始然后放大,你必须发出两个或更多的setRegion调用,因为setRegion它本身并不能让你控制起始区域或动画的速度。

viewDidLoad中,设置初始区域,span以便整个英国可见(尝试使用 delta10.0代替0.15)。您也可以设置animatedNO初始区域。

然后在 结束之前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];
于 2012-12-06T15:29:15.947 回答
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];
于 2014-06-09T06:48:33.447 回答