【问题标题】:How to put a button on a Map in Iphone如何在 Iphone 的地图上放置一个按钮
【发布时间】:2010-07-21 21:14:25
【问题描述】:

我正在尝试使用 MKMapView 使用界面构建器在我的应用程序中提取地图,但由于某种原因它没有显示出来。我还想通过单击可以浏览我的 iphone 中存在的文件来向此视图添加一些按钮。

由于我是新手,请提供详细说明。

谢谢,

【问题讨论】:

  • 您不能只使用 Interface Builder 简单地删除 pin。

标签: iphone objective-c mkmapview mkannotation


【解决方案1】:

您需要向地图添加注释,然后为其提供自定义视图。

要向地图添加注释,请在您的一个对象中采用 MKAnnotation 协议,并将其 coordinate 属性设置为适当的纬度/经度位置。

接下来,您将使用 MKMapView addAnnotation 将注释添加到地图中。

将地图的委托属性设置为您的视图控制器,然后实现 ma​​pView:viewForAnnotation:

当这个方法被调用时,为你的注解返回一个 MKAnnotationView。将 MKAnnotationView 的 image 属性设置为您希望注释使用的任何图像(也许是按钮的图像?)。

如果您想知道注释何时被选中,您可以实现 ma​​pView:didSelectAnnotationView:

您还可以使用 MKAnnotationView 的 leftCalloutAccessoryViewrightCalloutAccessoryView 属性在注释上设置标注附件按钮。如果您这样做,您可以通过实现 ma​​pView:annotationView:calloutAccessoryControlTapped: 在用户选择标注按钮时做出响应。

【讨论】:

  • 实际上我想要做的是,使用 MKMapView 使用界面构建器在我的应用程序中拉一张地图,但由于某种原因它没有显示出来。我还想通过单击可以浏览 iphone 中存在的文件来向此视图添加一些按钮。
【解决方案2】:
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *AnnotationViewID = @"annotationViewID";

     annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil)
    {
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
    }
    annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    annotationView.image = [UIImage imageNamed:@"s11.png"];

    annotationView.annotation = annotation;

    [annotationView setEnabled:YES];
    [annotationView setCanShowCallout:YES];

    return annotationView;
}

【讨论】:

  • 使用这个你可以在你的 pi 的注解中添加按钮,
  • 感谢您分享此信息,但我们如何才能通过拖放操作通过界面生成器简单地拉出地图。我做了同样的事情,但它没有出现。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-25
  • 1970-01-01
  • 2014-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-09
相关资源
最近更新 更多