How to open call out MKAnnotationView programmatically? (iPhone, MapKit)

后端 未结 2 2018
无人及你
无人及你 2020-12-08 03:05

I want to open up the callout for an MKPinAnnotationView programmatically. Eg I drop 10 pins on the map, and want to open up the one closest to me. How would I

相关标签:
2条回答
  • 2020-12-08 03:36

    In your mapViewController create an action method:

    - (void)openAnnotation:(id)annotation 
    {
        //mv is the mapView
        [mv selectAnnotation:annotation animated:YES];
    
    }
    

    You can then determine the closest annotation based on current location and walking the annotations available in the array.

    [mv annotations];
    

    Once the closest annotation is calculated, call:

    [self openAnnotation:closestAnnotation];
    

    The mapView should scroll automatically to place your annotation in the center of the display area.

    0 讨论(0)
  • 2020-12-08 03:44

    In swift 3 this is updated to:

    func openAnnotation(annotation: MkAnnotation) {
    _ = [mapView .selectAnnotation(annotation, animated: true)]
    }
    

    and can be called using any annotation (this will open the annotation callout view and attempt to center the annotation on the map)

    For example using the second annotation in a hypothetical list of annotations.

    openAnnotation(annotation: mapView.annotations[1])
    
    0 讨论(0)
提交回复
热议问题