Multiple annotation callouts displaying in MKMapView
Asked Answered
E

3

14

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.

Echinoderm answered 10/3, 2010 at 15:10 Comment(2)
Why more than 1? The screen size is not big enough and this confuses the user.Jarvey
That's right but if there're only two-three pins on map with enough big distance between them – user wouldn't be confused. And, yeah, that's my customer's requirement.Echinoderm
C
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:

- (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:

@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.

Circumambulate answered 24/6, 2010 at 18:41 Comment(2)
Sadly but true, as of 2013 and iOS 6.1, it is still as you describe.Ribald
@Ribald It's 2020 and iOS 13.4. I think still not solve a problemTout
K
7

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

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.

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 ?

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

  • don't rely on the selection mechanism of the MKMapView
  • subclass the MKAnnotationView to implement a custom one
  • do the customization in such a way that the callout is part of the annotation view so that you can display several of them.

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

Kress answered 10/3, 2010 at 16:9 Comment(2)
Thank you for your answer. Yeah, you're right, this may work.Echinoderm
I was essentially able to do this, but my callouts have buttons on them, and it seems that I can't click the buttons. Do you know what this is about?Vibrant
F
1

Since iOS 11, Apple added new annotation view called MKMarkerAnnotationView.

In func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?, when you dequeue the annotation view, make sure you cast it as MKMarkerAnnotationView (e.g. var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView), and set view's attributes titleVisibility and subtitleVisibility to .visible.

enter image description here

Fornof answered 25/10, 2019 at 2:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.