我在MKMapView
的一个单元格的一个单元格中设置了一个简单的CollectionView
(它只是为了在地图上显示一个位置,而不是更多),并且无法使用setRegion
设置其区域。 MKMapViewDelegat
e正确设置为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
由于某种原因被忽略或被其他东西覆盖。有什么想法吗?
我遇到过类似的情况,地图区域没有改变。所以我手动调用了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 {
}