在UICollectionViewCell中,setRegion对MKMapView没有影响

问题描述 投票:0回答:1

我在MKMapView的一个单元格的一个单元格中设置了一个简单的CollectionView(它只是为了在地图上显示一个位置,而不是更多),并且无法使用setRegion设置其区域。 MKMapViewDelegate正确设置为UICollectionViewCell,如所有委托方法中的断点所示。

我首先尝试在View Controller的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文件:

@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由于某种原因被忽略或被其他东西覆盖。有什么想法吗?

ios objective-c uicollectionview mkmapview uicollectionviewcell
1个回答
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 {
}
© www.soinside.com 2019 - 2024. All rights reserved.