How can the MKUserLocation be programmatically selected?

纵饮孤独 提交于 2019-12-10 19:37:46

问题


Titles and subtitles can be added to the user location that iOS shows using MKUserLocation. When the user taps on the location, these will show in a bubble above the location. The thought bubbles for other annotations can be shown by selecting the annotation with setSelected:animated: from MKAnnotationView. Unfortunately, MKUserLocation does not descend from MKAnnotationView.

How can I programmatically select the user location so the annotation appears over the user location without the user first tapping on it?


回答1:


The documentation for MKAnnotationView says this about its setSelected:animated: method (and something similar for its selected property):

You should not call this method directly.


Instead, use the MKMapView method selectAnnotation:animated:. If you call it in the didAddAnnotationViews delegate method, you can be sure the annotation view is ready to show the callout otherwise calling selectAnnotation will do nothing.

For example:

-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    for (MKAnnotationView *av in views)
    {
        if ([av.annotation isKindOfClass:[MKUserLocation class]])
        {
            [mapView selectAnnotation:av.annotation animated:NO];
            //Setting animated to YES for the user location 
            //gives strange results so setting it to NO.
            return;
        }
    }
}


来源:https://stackoverflow.com/questions/8824517/how-can-the-mkuserlocation-be-programmatically-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!