首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何知道MKMapview setRegion:animated:何时完成?

如何知道MKMapview setRegion:animated:何时完成?
EN

Stack Overflow用户
提问于 2010-01-18 03:19:00
回答 1查看 10.5K关注 0票数 25

我想在我的MKMapView上设置一个区域,然后找到与地图的NE和SW角相对应的坐标。

This code works just fine to do that:
//Recenter and zoom map in on search location
MKCoordinateRegion region =  {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center = mySearchLocation.searchLocation.coordinate;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[self.mapView setRegion:region animated:NO]; //When this is set to YES it seems to break the coordinate calculation because the map is in motion

//After the new search location has been added to the map, and the map zoomed, we need to update the search bounds
//First we need to calculate the corners of the map so we get the points
CGPoint nePoint = CGPointMake(self.mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
CGPoint swPoint = CGPointMake((self.mapView.bounds.origin.x), (mapView.bounds.origin.y + mapView.bounds.size.height));

//Then transform those point into lat,lng values
CLLocationCoordinate2D neCoord;
neCoord = [mapView convertPoint:nePoint toCoordinateFromView:mapView];
CLLocation *neLocation = [[CLLocation alloc] initWithLatitude:neCoord.latitude longitude:neCoord.longitude];

CLLocationCoordinate2D swCoord;
swCoord = [mapView convertPoint:swPoint toCoordinateFromView:mapView];
CLLocation *swLocation = [[CLLocation alloc] initWithLatitude:swCoord.latitude longitude:swCoord.longitude];

问题是我希望地图的缩放是动画的。但是,当我将setRegion:animated设置为YES时,我最终会在地图缩小时(即在动画完成之前)从地图中获取坐标。有没有办法得到动画完成的信号?

EN

回答 1

Stack Overflow用户

发布于 2016-07-30 08:23:30

我知道这很古老,但以防其他人来寻找答案,这里有一个替代方案。

这个版本的好处是,你可以在第一个完成的时刻运行完成动画,而不是在回调方法中猜测/硬编码它,因为那个回调方法会立即被调用。

[MKMapView animateWithDuration:1.0 animations:^{
    [mapView setRegion:mapRegion animated:YES];
} completion:^(BOOL finished) {
    [UIView animateWithDuration:1.0 animations:^{
        self.mapDotsImageView.alpha = 1.0;
    }];
}];

或者只是

// zoom in...
let km3:CLLocationDistance = 3000
let crTight = MKCoordinateRegionMakeWithDistance(location.coordinate, km3, km3)
MKMapView.animate(withDuration: 1.0, animations: { self.theMap.region = crTight })
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2082269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档