Monotouch MapKit - 添加按钮到标注气泡

7

有没有办法在注释上放置一个按钮?

我想让位置可选择 - 这样你可以选择位置,并通过点击按钮获取该位置的所有事件。

这种可能吗?

w://

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;
}

4
除此之外,您需要实现 - mapView:annotationView:calloutAccessoryControlTapped: 代理方法来处理注释气泡中披露按钮的点击。 - Brad Larson
2
为什么要释放 defaultPinID?你不必释放它。最后释放 pinAnnotation 是没有意义的,因为它从未被调用。 - testing
1
只是好奇 - 这里标记了Monotouch,为什么不使用C#示例呢? - Jack Marchetti

5
我刚帮助别人用Objective C完成了这个,但我确信在mono中的概念是相同的。您需要创建一个自定义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;
   }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接