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

后端 未结 13 2333
时光取名叫无心
时光取名叫无心 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:07

    Resetting the annotations also will bring the callout to front.

        [mapView removeAnnotation: currentMarker];
        [mapView addAnnotation:currentMarker];
    
    0 讨论(0)
  • 2020-11-29 20:08

    After trying a variety of answers to this thread, I finally came up with this. It works very reliably, I have yet to see it fail:

    - (void)mapView:(MKMapView *)aMapView didAddAnnotationViews:(NSArray *)views;
    {
        for(id<MKAnnotation> currentAnnotation in aMapView.annotations) 
        {       
            if([currentAnnotation isEqual:annotationToSelect]) 
            {
                NSLog(@"Yay!");
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_current_queue(), ^
                {
                    [aMapView selectAnnotation:currentAnnotation animated:YES];
                });
            }
        }
    }
    

    The block is used to delay slightly, as without it the callout may not be shown correctly.

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

    The problem with calling selectAnnotation from - (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView is that, as the name implies, this event is only triggered once your MapView loads initially, so you won't be able to trigger the annotation's callout if you add it after the MapView has finished loading.

    The problem with calling it from - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views is that your annotation may not be on-screen when selectAnnotation is called which would cause it to have no effect. Even if you center your MapView's region to the annotation's coordinate before adding the annotation, the slight delay it takes to set the MapView's region is enough for selectAnnotation to be called before the annotation is visible on-screen, especially if you animate setRegion.

    Some people have solved this issue by calling selectAnnotation after a delay as such:

    -(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
        [self performSelector:@selector(selectLastAnnotation)
            withObject:nil afterDelay:1];
    }
    
    -(void)selectLastAnnotation {
        [myMapView selectAnnotation:
            [[myMapView annotations] lastObject] animated:YES];
    }
    

    But even then you may get weird results since it may take more than one second for the annotation to appear on-screen depending on various factors like the distance between your previous MapView's region and the new one or your Internet connection speed.

    I decided to make the call from - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated instead since it ensures the annotation is actually on-screen (assuming you set your MapView's region to your annotation's coordinate) because this event is triggered after setRegion (and its animation) has finished. However, regionDidChangeAnimated is triggered whenever your MapView's region changes, including when the user just pans around the map so you have to make sure you have a condition to properly identify when is the right time to trigger the annotation's callout.

    Here's how I did it:

    MKPointAnnotation *myAnnotationWithCallout;
    
    - (void)someMethod {
        MKPointAnnotation *myAnnotation = [[MKPointAnnotation alloc] init];
        [myAnnotation setCoordinate: someCoordinate];
        [myAnnotation setTitle: someTitle];
    
        MKCoordinateRegion someRegion =
           MKCoordinateRegionMakeWithDistance (someCoordinate, zoomLevel, zoomLevel);
    
        myAnnotationWithCallout = myAnnotation;
        [myMapView setRegion: someRegion animated: YES];
        [myMapView addAnnotation: myAnnotation];
    }
    
    - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
    {
        if (myAnnotationWithCallout)
        {
            [mapView selectAnnotation: myAnnotationWithCallout animated:YES];
            myAnnotationWithCallout = nil;
        }
    }
    

    That way your annotation is guaranteed to be on-screen at the moment selectAnnotation is called, and the if (myAnnotationWithCallout) part ensures no region setting other than the one in - (void)someMethod will trigger the callout.

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

    Ok, here's the solution to this problem.

    To display the callout use MKMapView's selectAnnotation:animated method.

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

    Assuming that you want the last annotation view to be selected, you can put the code below:

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

    in the delegate below:

    - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
    {
        //Here
        [mapView selectAnnotation:[[mapView annotations] lastObject] animated:YES];
    }
    
    0 讨论(0)
  • 2020-11-29 20:12

    Due to something like the code shown by benvolioT, that I suspect exists in the system, when I used selectAnnotation:animation: method, it did not show the callOut, I guessed that the reason was because it was already selected and it was avoiding from asking the MapView to redraw the callOut on the map using the annotation title and subtitle.

    So, the solution was simply to deselect it first and to re-select it.

    E.g: First, I needed to do this in Apple's touchMoved method (i.e. how to drag an AnnotationView) to hide the callOut. (Simply using annotation.canShowAnnotation = NO alone does not work, since I suspect that it needs redrawing. The deselectAnnotaiton causes the necessary action. Also, deselecting alone did not do that trick, the callOut disappeared only once and got redrawn straight away. This was the hint that it got reselected automatically).

    annotationView.canShowAnnotation = NO;
    [mapView deselectAnnotation:annotation animated:YES];
    

    Then, simply using the code below in touchEnded method did not bring back the callOut (The annotation has been automatically selected by the system by that time, and presumably the redrawing of the callOut never occrrs):

    annotationView.canShowAnnotation = YES;
    [mapView selectAnnotation:annotation animated:YES];
    

    The solution was:

    annotationView.canShowAnnotation = YES;
    [mapView deselectAnnotation:annotation animated:YES];
    [mapView selectAnnotation:annotation animated:YES];
    

    This simply bought back the callOut, presumably it re-initiated the process of redrawing the callOut by the mapView.

    Strictly speaking, I should detect whether the annotation is the current annotation or not (selected, which I know it is) and whether the callOut is actually showing or not (which I don't know) and decide to redraw it accordingly, that would be better. I, however, have not found the callOut detection method yet and trying to do so myself is just a little bit unnecessary at this stage.

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