7

任何人都知道是否有任何方法可以在注释上添加按钮?

我希望该位置是可选择的 - 所以您可以说.. 选择该位置并通过单击按钮获取该位置的所有事件。

这可能吗?

w://

4

2 回答 2

13

这是我用于注释的代码,它在气泡右侧包含一个按钮。您可以设置 IBAction 以将新视图推送到堆栈上以显示您想要的任何内容

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinAnnotation = nil;
    if(annotation != mapView.userLocation) 
    {
        static NSString *defaultPinID = @"myPin";
        pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinAnnotation == nil )
            pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinAnnotation.canShowCallout = YES;

        //instatiate a detail-disclosure button and set it to appear on right side of annotation
        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinAnnotation.rightCalloutAccessoryView = infoButton;

    }

    return pinAnnotation;
}
于 2010-03-02T19:39:34.253 回答
5

我只是在目标 c 中帮助了其他人,但我确信这个概念与单声道相同。您需要创建一个自定义 MKAnnotationView 对象并覆盖 MKMapViewDelegate 类的 GetViewForAnnotation obj-c 中的 viewForAnnotation)方法...查看其他问题

当您创建自定义 MKAnnotationView 对象时,它基本上是为地图注释制作的 UIView ...您只需将按钮和其他信息添加到视图中,当用户点击注释时它就会显示出来。

这是委托方法的一些粗略代码:

public override MKAnnotationView GetViewForAnnotation(
                                         MKMapView mapView,NSObject annotation) {
      var annotationId = "location";
      var annotationView = mapView.DequeueReusableAnnotation(annotationId);
      if (annotationView == null) {
         // create new annotation
         annotationView = new CustomAnnotationView(annotation, annotationId);
      }
      else {
         annotationView.annotation = annotation;
      }
      annotation.CanShowCallout = true;
      // setup other info for view
      // ..........

      return annotationView;
   }
}
于 2010-03-02T17:30:16.677 回答