在我的应用程序中,我有一个表格视图,可以切换地图视图上的注释。我可以通过标签栏控制器在表格视图和地图视图之间来回切换。我的地图将重新加载视图上的注释(来自表格视图中的选定项目)确实出现了。我需要知道这些注释何时完成加载,以便我可以运行我的方法来缩放到由注释集群确定的生成区域。
问题是当我在视图中的 addAnnotations 方法确实出现之后直接运行缩放方法时,它会在我的注释获得正确的坐标之前开始缩放过程。从而导致不正确的缩放,并且我的注释移动到正确的位置。
我还想指出,我正在使用前向地理编码来获取我的注释坐标。
这是我的观点确实出现了:
[super viewDidAppear:animated];
[businessMap removeAnnotations:businessPoints];
[businessPoints removeAllObjects];
UINavigationController *navVC = (UINavigationController *) [self.tabBarController.viewControllers objectAtIndex:0];
FirstViewController *VC = [navVC.viewControllers objectAtIndex:0];
for (businessInfo *business_info in VC.selectedBusinessesArray) {
businessInfoAnnotation *businessAnnotation = [[businessInfoAnnotation alloc] init];
businessAnnotation.businessInfoClass = business_info;
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:business_info.location
completionHandler:^(NSArray* geocoded, NSError* error){
if (geocoded && geocoded.count > 0) {
CLPlacemark *placemark = [geocoded objectAtIndex:0];
CLLocation *location = placemark.location;
CLLocationCoordinate2D business_cords = location.coordinate;
businessAnnotation.coordinate = business_cords;
}
}];
businessAnnotation.title = business_info.name;
[businessPoints addObject:businessAnnotation];
}
[businessMap addAnnotations:businessPoints];
[businessMap setZoomEnabled:YES];
[self zoomToFitMapAnnotations:businessMap withArray:businessPoints];
这是我的缩放方法:
-(void)zoomToFitMapAnnotations:(MKMapView*)mapViews withArray:(NSArray*)anAnnotationArray
{
if([mapViews.annotations count] == 0) return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
NSLog(@"zooming");
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(MKPointAnnotation* annotation in anAnnotationArray)
{
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}
MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
region = [mapViews regionThatFits:region];
[businessMap setRegion:region animated:YES];
}
感谢您的帮助,谢谢。抱歉,如果这是草率,这是我的第一篇文章。
编辑:
这是我根据 nevan king 的回答编辑的地理编码器方法。
[geocoder geocodeAddressString:business_info.location
completionHandler:^(NSArray* geocoded, NSError* error){
if (geocoded && geocoded.count > 0) {
CLPlacemark *placemark = [geocoded objectAtIndex:0];
CLLocation *location = placemark.location;
CLLocationCoordinate2D business_cords = location.coordinate;
businessAnnotation.coordinate = business_cords;
businessAnnotation.title = business_info.name;
[businessPoints addObject:businessAnnotation];
[businessMap addAnnotations:businessPoints];
[self zoomToFitMapAnnotations:businessMap withArray:businessPoints];
}
}
];
也不是我尝试使用注释数组的计数来确定最后一个注释的地理编码完成处理程序以仅更改该特定区域的区域。但这对我所在的地区产生了不一致的结果。这是它始终将所有注释保持在视图中的唯一方法。