如何在加载地图时直接显示多个注释的标题

我将多个注释加载到我的地图视图中。 这些注释在加载地图时显示为一个pin。

通过使用下面的代码,我可以直接显示一个标注的标题,但是对于其余的标注,用户需要点击标题以查看标题。

我想在Mapview Loaded时显示所有注解的标题而不触及注释引脚

NSMutableArray * locations = [[NSMutableArray alloc] init]; _myAnn = [[MKPointAnnotation alloc] init]; _locationCoordinate.latitude = 27.175015; _locationCoordinate.longitude = 78.042155; _myAnn.coordinate = _locationCoordinate; _myAnn.title = @"A"; [locations addObject:_myAnn]; _myAnn = [[MKPointAnnotation alloc] init]; _locationCoordinate.latitude = 28.171391; _locationCoordinate.longitude = 79.037090; _myAnn.coordinate = _locationCoordinate; _myAnn.title = @"B"; [locations addObject:_myAnn]; _myAnn = [[MKPointAnnotation alloc] init]; _locationCoordinate.latitude = 29.169005; _locationCoordinate.longitude = 80.043206; _myAnn.coordinate = _locationCoordinate; _myAnn.title = @"C "; [locations addObject:_myAnn]; [self.map_View addAnnotations:locations]; // i am using below method to display title for one annotation // [self.map_View selectAnnotation:_myAnn animated:YES]; for (MKPointAnnotation *annotation in locations) { [self.map_View selectAnnotation:annotation animated:NO]; } MKCoordinateSpan span; Span. latitudeDelta = 10; Span. longitudeDelta = 10; MKCoordinateRegion region; region.span = span; region.center = _locationCoordinate; [self.map_View setRegion:region animated:YES]; 

提前致谢..

更新它确实似乎这是不可能的 – 至less不与地图视图批注API。 尽pipeselect和取消select方法的存在以及选定的注释属性是一个数组,但似乎一次只能select一个注释。

它看起来像你需要创build一个自定义的注释视图,在这个答案中build议 – 在MKMapView中显示多个注释标注

根据MKAnnotationView类引用 ,注释标注气泡在selected属性更改值时显示并消失。 该文件指出,开发人员“不应该直接设置这个属性的值”,但是你可以试试:)

第一个我会尝试的是依赖于MKAnnotationView属性的最简单的方法。 在你的MKMapViewDelegate类中,定义下面的方法:

 - (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view { view.selected = YES; } 

但是,我猜这可能不太好,因为在点击不同的注释时出现视觉伪影。 如果这是真的,那么唯一正确的方法就是创buildMKAnnotationView子类,它总是显示一个标注泡泡,而不pipeselected属性的值如何。 以下是关于如何创build自定义注释视图的一些教程:

更新

感谢@ Paulw11的回答 ,我忘了提到为了使“hacky”方法起作用,您需要为所有注释设置select值。 这可以通过3种方式完成:

  1. 根据@ Paulw11的回答 (但这不适用于地图可见矩形外的注释)
  2. 在您的MKMapViewDelegatemapView:viewForAnnotation:方法的实现中设置MKAnnotationViewselected属性(不知道这是否可行)
  3. 每当地图视图可见区域更改时,将MKAnnotationViewselected属性设置为可见注释。

使用第三种方法,我需要实现MKMapViewDelegatemapView:regionDidChangeAnimated:方法来处理地图视图可见区域的更改。 由于这个委托方法在用户拖动或放大/缩小时经常被调用,所以推迟注解视图的实际更新是有意义的,直到地图视图的可见区域改变结束为止。 要做到这一点,我会使用NSTimer。

 // In an interface or interface extension for MKMapViewDelegate class (guess this // is your view controller), declare NSTimer and a method which will be executed // when a user stops dragging or zooming @property (nonatomic, strong) NSTimer* mapViewRegionChangeTimer; - (void)mapViewRegionChangeCompleted:(NSTimer*)timer; // In your implementation of MKMapViewDelegate: - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated { if (self.mapViewRegionChangeTimer != nil) { [self.mapViewRegionChangeTimer invalidate]; } self.mapViewRegionChangeTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(mapViewRegionChangeCompleted:) userInfo:nil repeats:NO]; } - (void)mapViewRegionChangeCompleted:(NSTimer*)timer { [self.mapViewRegionChangeTimer invalidate]; self.mapViewRegionChangeTimer = nil; NSSet* visibleAnnotations = [self.map_View annotationsInMapRect:self.map_View.visibleMapRect]; for (id<MKAnnotation> annotation in visibleAnnotations) { [self.map_View selectAnnotation:annotation animated:NO]; } }