我正在将多个注释加载到我的地图视图中。加载地图时,这些注释显示为引脚。
通过使用以下代码,我可以直接为一个注释显示标题,但是对于剩余的注释,用户需要点击引脚以查看标题。
我希望在没有触及注释引脚的情况下显示所有注释的标题
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];
先谢谢..
答案 0 :(得分:1)
更新这似乎确实无法实现 - 至少没有地图视图注释API。尽管存在select和deselect方法,并且选定的注释属性是一个数组,但似乎一次只能选择一个注释。
您似乎需要按照此答案中的建议创建自定义注释视图 - Multiple annotation callouts displaying in MKMapView
答案 1 :(得分:0)
根据MKAnnotationView Class Reference,注释标注气泡出现&在selected
属性的更改值消失。该文档声明开发人员不应直接设置此属性的值",但您可以尝试:)
我首先尝试的是依赖MKAnnotationView
属性的最简单的hacky方式。在MKMapViewDelegate
课程中,定义以下方法:
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
view.selected = YES;
}
但我想这可能不会很好,因为在点击不同的注释时会出现视觉伪影。如果这是真的,那么实现所需的唯一正确方法是创建MKAnnotationView
的子类,它始终显示一个标注气泡,而不管selected
属性的值。以下是有关如何创建自定义注释视图的一些教程:
感谢@ Paulw11的answer,我忘记提及要做出“hacky'方法工作,您需要为所有注释设置select
值。这可以通过3种方式完成:
MKAnnotationView
selected
方法的实施中设置MKMapViewDelegate
' mapView:viewForAnnotation:
属性(不确定这是否有效)< / LI>
MKAnnotationView
&#39; selected
属性设置可见注释。 使用第3种方法,我们需要实施处理地图视图可见区域更改的MKMapViewDelegate
mapView: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];
}
}