在UICollectionViewCell中,setRegion对MKMapView没有影响

时间:2015-01-07 01:21:11

标签: ios objective-c uicollectionview mkmapview uicollectionviewcell

我在MKMapView的某个区域的一个单元格内设置了一个简单的CollectionView(仅用于在地图上显示位置,仅此而已),并且无法设置其区域使用setRegionMKMapViewDelegat e正确设置为UICollectionViewCell,如所有委托方法中的断点所示。

我首先尝试在视图控制器cellForItemAtIndexPath设置区域,但我猜这太早了,即在加载地图之前没有发生,它加载在海洋中间的默认位置。

我已尝试在CLCoordinateLocation2D期间将CLCoordinateLocation2D从视图控制器传递到单元格中的cellForItemAtIndexPath,以设置单元格内的区域,但设置区域的适当位置在哪里?在ViewController中,应该在ViewDidAppear而不是ViewDidLoad或ViewWillAppear中完成,以便加载地图,但我找不到collectionviewcell的等效事件。 awakeFromNib显然太早了。我尝试过使用各种mapView代理,例如:

- (void)mapView:(MKMapView *)mapView1
regionWillChangeAnimated:(BOOL)animated
{
    MKCoordinateSpan span = {.latitudeDelta =  0.0025, .longitudeDelta =  0.0025};
MKCoordinateRegion adjustedRegion = [mapView1     regionThatFits:MKCoordinateRegionMakeWithDistance(coords, 200, 200)];
  [mapView1 setRegion:adjustedRegion animated:YES];
}

运行但又不会改变该区域。我试过了

- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView and
- (void)mapViewWillStartRenderingMap:(MKMapView *)mapView

这似乎是最好的赌注,但setRegion行之前和之后的断点显示该区域永远不会更新到我设置的区域。所有事件都应该在地图完全加载的时候,为什么setRegion根本没有效果?

单元格文件:

@interface BasicsCollectionViewCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic) CLLocationCoordinate2D coords;
@end

Cell m file:

@interface BasicsCollectionViewCell() <MKMapViewDelegate>
@end

@implementation BasicsCollectionViewCell
@synthesize mapView;
@synthesize coords;

 - (void)mapView:(MKMapView *)mapView1
regionWillChangeAnimated:(BOOL)animated
{
    MKCoordinateSpan span = {.latitudeDelta =  0.0025, .longitudeDelta =  0.0025};
MKCoordinateRegion adjustedRegion = [mapView1     regionThatFits:MKCoordinateRegionMakeWithDistance(coords, 200, 200)];
  [mapView1 setRegion:adjustedRegion animated:YES];
}

cellForItemAtIndexPath:

的相关部分
 cell0 = (BasicsCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"BasicsCell" forIndexPath:indexPath];
    CLLocationCoordinate2D coord = {.latitude = self.latitude, .longitude = self.longitude};
    [cell0 setCoords:coord];
    cell0.mapView.delegate = cell0;
    MKCoordinateSpan span = {.latitudeDelta =  0.0025, .longitudeDelta =  0.0025};
    MKCoordinateRegion adjustedRegion = [cell0.mapView regionThatFits:MKCoordinateRegionMakeWithDistance(coord, 200, 200)];
    [cell0.mapView setRegion:adjustedRegion animated:YES];
    return cell0;

编辑:

如果我在星号点setRegion行之前和之后的代码中添加断点...

 - (void)mapViewWillStartRenderingMap:(MKMapView *)mapView
{
    MKCoordinateSpan span = {.latitudeDelta =  0.0025, .longitudeDelta =  0.0025};
    MKCoordinateRegion adjustedRegion = [mapView     regionThatFits:MKCoordinateRegionMakeWithDistance(coords, 200, 200)];
  *****[mapView setRegion:adjustedRegion animated:YES];
  *****}

然后调试器在第一个断点处给出以下内容:

po self.mapView.region
 {
   (latitude = -41.508575000000008, longitude = 174.19921900000011)
   (latitudeDelta = 13.733789001832008, longitudeDelta = 59.305518116231042)
}

在紧接着的断点处也一样:

po self.mapView.region
 {
   (latitude = -41.508575000000008, longitude = 174.19921900000011)
   (latitudeDelta = 13.733789001832008, longitudeDelta = 59.305518116231042)
}

在两个断点处,以下是adjustedRegion:

的(正确)值
po adjustedRegion
     {
      (latitude = -0.13506928037132482, longitude = 51.518398284912109)
      (latitudeDelta = 0.0018087389042361623, longitudeDelta = 0.0058458603824931288)
    }

因此,由于某种原因,setRegion被忽略或者被其他东西覆盖了。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我遇到过类似的情况,地图区域没有改变。所以我手动调用了mapView委托方法

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;

在视图控制器的- (void) viewDidAppear:(BOOL)animated中。

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    // Manually called delegate method to forcefully set region.
    [self mapView:self.mapView regionDidChangeAnimated:YES];
}

在您的情况下,在设置地图区域后立即调用此方法。从上面的代码中可以看出cellForItemAtIndexPath方法。例如:

cell0 = (BasicsCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"BasicsCell" forIndexPath:indexPath];
CLLocationCoordinate2D coord = {.latitude = self.latitude, .longitude = self.longitude};
[cell0 setCoords:coord];
cell0.mapView.delegate = cell0;
MKCoordinateSpan span = {.latitudeDelta =  0.0025, .longitudeDelta =  0.0025};
MKCoordinateRegion adjustedRegion = [cell0.mapView regionThatFits:MKCoordinateRegionMakeWithDistance(coord, 200, 200)];
[cell0.mapView setRegion:adjustedRegion animated:YES];

[cell0.mapView setRegion:adjustedRegion animated:YES];

// Force call of delegate method
[self mapView:cell0.mapView regionDidChangeAnimated:YES];


return cell0;

注意:

不要忘记在视图控制器中覆盖regionDidChangeAnimated方法。

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
}