我在xcode中有一个mapview,它运行得很好。
我的页面刚才做的是这样的:
这一切都很好,我最终得到了一个有大量引脚的地图视图和一个包含这些引脚细节的表视图。
我现在要做的是,允许用户点击表视图中的一行,并使地图缩放和中心到相应的地图引脚,然后自动激活注释引脚标注。
在我的“didselectrow”方法中,我有以下内容:
MKCoordinateSpan span = MKCoordinateSpanMake(0.1f, 0.1f);
CLLocationCoordinate2D coordinate = { [item.latitude floatValue], [item.longitude floatValue] };
MKCoordinateRegion region = { coordinate, span };
[self.mapView setRegion:region animated:YES];这个也很好用。点击桌子行将放大到和中间的地图针在这个位置。
我只是无法完成触发注释引脚标注的最后一步。
我试过:
[mapview annotationsInMapRect:mapview.visibleMapRect];但这是行不通的,而且可能仍然有2或3个图钉在可见区域。
我需要做的是让销最近的中心位置(见上面- item.latitude / item.longitude)自动打开它的标注。
代码中的所有内容都是设置和工作的,而地图引脚有呼号,当被点击时,我只需要最后一个阶段,让最接近中心位置的引脚自动打开。
有人能帮忙吗?
我曾尝试过其他各种建议,但似乎都不符合这一要求。
发布于 2015-10-24 11:54:46
我想我已经解决了你的问题--你需要使用这个[_mapView setSelectedAnnotations:@[[[self.mapView annotations] lastObject]]];
对于测试,我有创建了一个具有以下2种方法的小项目。
- (IBAction)buttonTouched:(id)sender {
[_mapView showAnnotations:[self.mapView annotations] animated:YES];
[self performSelector:@selector(showAnnotationCallOut) withObject:nil afterDelay:1.0f];
}
- (void) showAnnotationCallOut {
[_mapView setSelectedAnnotations:@[[[self.mapView annotations] lastObject]]];
}注意:我只调用了一个注释来测试为什么要调用最后一个对象。您需要调用它作为注释数组的特定注释。
编辑:根据里克普的评论,是解决这个问题的方法。
for (MapViewAnnotation *annotion in [self.mapView annotion]) {
if ([annotion.identifire isEqualToString:annotationToCallCallOutIdentifier]) {
//[_mapView setSelectedAnnotations:@[annotation]];
[_mapView selectAnnotation:annotation animated:YES];
break;//don't break if there are can be more than one callouts
}
}https://stackoverflow.com/questions/33317341
复制相似问题