在MKMapView中显示多个注释标注

时间:2021-04-11 19:56:50

Is it possible to open simultaneously more then one callout?

是否可以同时打开多个标注?

The code:

代码:

- (void)mapViewDidFinishLoadingMap:(MKMapView *)theMapView {
    for (id<MKAnnotation> currentAnnotation in theMapView.annotations) {
        [theMapView selectAnnotation:currentAnnotation animated:YES];
    }
}

opens only one callout.

只打开一个标注。

2 个解决方案

#1


9  

Note that there is a method on MKMapView (not MKAnnotationView) for selecting an annotation programmatically that works more or less as you would expect:

请注意,MKMapView(而不是MKAnnotationView)上有一个方法可以以编程方式选择注释,它或多或少地按预期工作:

- (void)selectAnnotation:(id < MKAnnotation >)annotation animated:(BOOL)animated

However, it automatically deselects any currently annotation at the same time so this doesn't solve your problem.

但是,它会同时自动取消选择任何当前注释,因此这不能解决您的问题。

Oddly, there is a property on MKMapView that appears to hold an array of currently selected annotations:

奇怪的是,MKMapView上有一个属性,它似乎包含一系列当前选定的注释:

@property(nonatomic, copy) NSArray *selectedAnnotations

But the documentation on this method says:

但是这个方法的文档说:

"Assigning a new array to this property selects the first annotation in the array only."

“为此属性指定一个新数组仅选择数组中的第一个注释。”

Just thought this might be of interest.

只是觉得这可能是有意义的。

#2


7  

From a strict API perspective, this does not seem possible.

从严格的API角度来看,这似乎是不可能的。

The -(void)setSelected:(BOOL)selected animated:(BOOL)animated selector on MKAnnotationView states : "You should not call this method directly. An MKMapView object calls this method in response to user interactions with the annotation." so the underlying message is that the selection of annotationView instances in under the full responsability of user selection, and as the user can only select one of them at a time, you shouldn't be able to get several of them selected at the same time.

- (void)setSelected:(BOOL)在MKAnnotationView上选择了动画:(BOOL)动画选择器:“你不应该直接调用这个方法.MKMapView对象调用这个方法来响应用户与注释的交互。”所以基础信息是在用户选择的完全负责下选择annotationView实例,并且由于用户一次只能选择其中一个,你不应该同时选择其中几个。

Even if the documentation says that should not call this method directly, did you try to invoke it anyway with setSelected:YES on several MKAnnotationView instances to see what it gives ?

即使文档说不应该直接调用此方法,您是否尝试使用几个MKAnnotationView实例上的setSelected:YES来调用它以查看它给出的内容?

THE CLEAN WAY I WOULD DO IT : (not tested myself however)

我会做的清洁方式:(不测试自己)

  • don't rely on the selection mechanism of the MKMapView
  • 不要依赖MKMapView的选择机制
  • subclass the MKAnnotationView to implement a custom one
  • 子类MKAnnotationView实现自定义
  • do the customization in such a way that the callout is part of the annotation view so that you can display several of them.
  • 以这样的方式进行自定义,即callout是注释视图的一部分,以便您可以显示其中的几个。

If you do it like this, you can make appear several callout bubble at the same time and get something that would look like :

如果你这样做,你可以同时出现几个标注气泡,并得到一些看起来像:

alt text http://a1.phobos.apple.com/us/r1000/048/Purple/2b/b2/ec/mzl.ttcsrlee.480x480-75.jpg

替代文字http://a1.phobos.apple.com/us/r1000/048/Purple/2b/b2/ec/mzl.ttcsrlee.480x480-75.jpg

#1


9  

Note that there is a method on MKMapView (not MKAnnotationView) for selecting an annotation programmatically that works more or less as you would expect:

请注意,MKMapView(而不是MKAnnotationView)上有一个方法可以以编程方式选择注释,它或多或少地按预期工作:

- (void)selectAnnotation:(id < MKAnnotation >)annotation animated:(BOOL)animated

However, it automatically deselects any currently annotation at the same time so this doesn't solve your problem.

但是,它会同时自动取消选择任何当前注释,因此这不能解决您的问题。

Oddly, there is a property on MKMapView that appears to hold an array of currently selected annotations:

奇怪的是,MKMapView上有一个属性,它似乎包含一系列当前选定的注释:

@property(nonatomic, copy) NSArray *selectedAnnotations

But the documentation on this method says:

但是这个方法的文档说:

"Assigning a new array to this property selects the first annotation in the array only."

“为此属性指定一个新数组仅选择数组中的第一个注释。”

Just thought this might be of interest.

只是觉得这可能是有意义的。

#2


7  

From a strict API perspective, this does not seem possible.

从严格的API角度来看,这似乎是不可能的。

The -(void)setSelected:(BOOL)selected animated:(BOOL)animated selector on MKAnnotationView states : "You should not call this method directly. An MKMapView object calls this method in response to user interactions with the annotation." so the underlying message is that the selection of annotationView instances in under the full responsability of user selection, and as the user can only select one of them at a time, you shouldn't be able to get several of them selected at the same time.

- (void)setSelected:(BOOL)在MKAnnotationView上选择了动画:(BOOL)动画选择器:“你不应该直接调用这个方法.MKMapView对象调用这个方法来响应用户与注释的交互。”所以基础信息是在用户选择的完全负责下选择annotationView实例,并且由于用户一次只能选择其中一个,你不应该同时选择其中几个。

Even if the documentation says that should not call this method directly, did you try to invoke it anyway with setSelected:YES on several MKAnnotationView instances to see what it gives ?

即使文档说不应该直接调用此方法,您是否尝试使用几个MKAnnotationView实例上的setSelected:YES来调用它以查看它给出的内容?

THE CLEAN WAY I WOULD DO IT : (not tested myself however)

我会做的清洁方式:(不测试自己)

  • don't rely on the selection mechanism of the MKMapView
  • 不要依赖MKMapView的选择机制
  • subclass the MKAnnotationView to implement a custom one
  • 子类MKAnnotationView实现自定义
  • do the customization in such a way that the callout is part of the annotation view so that you can display several of them.
  • 以这样的方式进行自定义,即callout是注释视图的一部分,以便您可以显示其中的几个。

If you do it like this, you can make appear several callout bubble at the same time and get something that would look like :

如果你这样做,你可以同时出现几个标注气泡,并得到一些看起来像:

alt text http://a1.phobos.apple.com/us/r1000/048/Purple/2b/b2/ec/mzl.ttcsrlee.480x480-75.jpg

替代文字http://a1.phobos.apple.com/us/r1000/048/Purple/2b/b2/ec/mzl.ttcsrlee.480x480-75.jpg