如何在当前位置标注上放置正确的附件按钮?

Ala*_*ore 2 gps mkmapview mkannotation mkannotationview ios

我想在当前用户位置标注中添加正确的附件按钮.我发现了一些关于这个主题的问题,但它们似乎对我没用.这是我到目前为止的代码,我已经成功地将正确的附件按钮放到我的其他注释上:

- (MKAnnotationView *)mapView:(MKMapView *)_mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
        //TODO: this doesn't seem to be working?  
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        MKAnnotationView * aV = [mapView viewForAnnotation:annotation];
        aV.canShowCallout = YES;
        aV.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        return nil;
    }
    return nil;
}
Run Code Online (Sandbox Code Playgroud)

我在几个地方读过,解决方案是为用户位置制作一个自定义的MKAnnotationView,但我还没有看到如何为用户位置做这个演示的演示 - 我已经成功地为其他注释做了这个我的地图.

示例代码将是最有帮助的,谢谢!

cha*_*tur 9

您应该再次获得用户位置注释视图的参考.在iOS> = 5中调用以下方法

选项1

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    MKAnnotationView *aV; 
     for (aV in views) {
           if ([aV.annotation isKindOfClass:[MKUserLocation class]]) {
                MKAnnotationView* annotationView = aV;
                annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            }
     }
}
Run Code Online (Sandbox Code Playgroud)

请参阅下面的委托方法

替代方法

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
     MKAnnotationView* annotationView = [mapView viewForAnnotation:userLocation];   
     annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
Run Code Online (Sandbox Code Playgroud)

确保添加委托方法,以便在点击标注按钮时执行任何操作:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
Run Code Online (Sandbox Code Playgroud)

在任何iOS中,这些方法中的任何一个都被调用,您可以向其添加右/左辅助按钮.通过检查用户位置注释视图的子视图计数,确保不要添加此按钮两次.

您也可以在运行时添加/删除附件视图,但我相信您不会想要删除它.按钮