How to trigger MKAnnotationView's callout view without touching the pin?

后端 未结 13 2334
时光取名叫无心
时光取名叫无心 2020-11-29 19:57

I\'m working on a MKMapView with the usual colored pin as the location points. I would like to be able to have the callout displayed without touching the pin.

相关标签:
13条回答
  • 2020-11-29 20:14

    Steve Shi's response made it clear to me that selectAnnotation has to be called from mapViewDidFinishLoadingMap method. Unfortunately i cannot vote up but i want to say thanks here.

    0 讨论(0)
  • 2020-11-29 20:14

    Just add [mapView selectAnnotation:point animated:YES];

    0 讨论(0)
  • 2020-11-29 20:16

    But there is a catch to get benvolioT's solution to work, the code

    for (id<MKAnnotation> currentAnnotation in mapView.annotations) {       
        if ([currentAnnotation isEqual:annotationToSelect]) {
            [mapView selectAnnotation:currentAnnotation animated:FALSE];
        }
    }
    

    should be called from - (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView, and nowhere else.

    The sequence in which the various methods like viewWillAppear, viewDidAppear of UIViewController and the - (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView is called is different between the first time the map is loaded with one particular location and the subsequent times the map is displayed with the same location. This is a bit tricky.

    0 讨论(0)
  • 2020-11-29 20:22

    If you just want to open the callout for the last annotation you added, try this, works for me.

    [mapView selectAnnotation:[[mapView annotations] lastObject] animated:YES];

    0 讨论(0)
  • 2020-11-29 20:26

    I read the API carefully and finally I found the problem:


    If the specified annotation is not onscreen, and therefore does not have an associated annotation view, this method has no effect.

    So you can wait some time (for example, 3 seconds) and then perform this action. Then it works.

    0 讨论(0)
  • 2020-11-29 20:29

    This does not work for me. I suspect a bug in the MapKit API.

    See this link for details of someone else for who this is not working: http://www.iphonedevsdk.com/forum/iphone-sdk-development/19740-trigger-mkannotationview-callout-bubble.html#post110447

    --edit--

    Okay after screwing with this for a while, here is what I've been able to make work:

    for (id<MKAnnotation> currentAnnotation in mapView.annotations) {       
        if ([currentAnnotation isEqual:annotationToSelect]) {
            [mapView selectAnnotation:currentAnnotation animated:FALSE];
        }
    }
    

    Note, this requires implementing - (BOOL)isEqual:(id)anObject for your class that implements the MKAnnotation protocol.

    0 讨论(0)
提交回复
热议问题