【问题标题】:Perform backend operations on MKPointAnnotation对 MKPointAnnotation 执行后端操作
【发布时间】:2012-05-18 19:11:10
【问题描述】:

我使用 MKPointAnnotation(s) 在 MKMapView 上填充了几个位置。在点击注释时,我会使用 UIActionSheet 菜单显示一些选项。这些选项具有一些删除功能,当用户点击 UIActionSheet 上的删除选项时,将删除地图上选定的注释。问题是我无法确定点击了哪个注释点,我似乎没有参考它。

添加注解点的代码是:

while(looping array of locations)
{
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = {coord of my location}
annotationPoint.title = [anObject objectForKey:@"castTitle"];
annotationPoint.subtitle = [anObject objectForKey:@"storeName"];

[self.mainMapView addAnnotation:annotationPoint];
}

点击注释时显示 UIActionSheet 的代码是:

-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    if([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];

    pinView.animatesDrop = YES;
    pinView.canShowCallout = YES;
    pinView.pinColor = [self getAnnotationColor];

    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(showOptions:) forControlEvents:UIControlEventTouchUpInside];
    pinView.rightCalloutAccessoryView = rightButton;

    return pinView;
}

-(IBAction)showOptions:(id)sender
{
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"", @"") delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel") destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"Delete", @"Delete"),      nil];

        [sheet showInView:[self.view window]];
}

【问题讨论】:

    标签: ios ios5 mkmapview mkannotation mkannotationview


    【解决方案1】:

    好像有两种方法。

    如果一次只能选择一个注释,您可以访问封闭的MKMapView-selectedAnnotations 属性。

    另一种方法是检查showOptions: 中的sender,这是对触发操作的UIButton 的引用。找出其封闭的MKAnnotationView,这将为您提供相关的-annotation。然后,您可以将其作为 ivar 存储,或者(我的首选方法)使用一些运行时魔法 - 以 objc_setAssociatedObject() 的形式,在 &lt;objc/runtime.h&gt; 中声明 - 将对注释的引用附加到操作表,以便在它的委托回调。

    (如果需要,您实际上可以在按钮创建阶段执行此操作,并将对注释的引用附加到 UIButton,可以直接在 showOptions: 中获取并重新附加到操作表。

    [MKMapView selectedAnnotations] 如果适合您的需求,我认为这是更简单的方法。

    【讨论】:

    • 我使用了 selectedAnnotations。我可以得到 MKPointAnnotation 的标题和副标题,但是为了准确地确定在其他控件中应该有一个像“标签”这样的 ID 属性,因为标题和副标题不是唯一值,因为我必须更改基于后端数组在这个选定的注释上。
    • 刚刚通过覆盖 MKPointAnnotation 类并向其添加 ID 属性解决了这个问题。谢谢
    【解决方案2】:

    您还可以创建一个继承自 MKPointAnnotation 的类,并添加一个 id 属性,然后使用您的类:

    @interface MyPointAnnotation : MKPointAnnotation 
    
    @property int pointId;
    
    @end
    

    然后在你的视图控制器中使用它:

    创建注释:

        MyPointAnnotation *myAnnotation = [[MyPointAnnotation alloc]init];
        myAnnotation.coordinate= someCoordinates;
        [myAnnotation setTitle:@"i am annotation with id"];
        myAnnotation.pointId = 1;
        [self.mapView addAnnotation:myAnnotation];
    

    如果你有一个坐标数组,你可以循环并创建注释。

    你甚至可以通过 id 自定义注解视图:

    -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    
    MKPinAnnotationView *view=(MKPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"reuseme"];
      if (!view) {
          view=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"reuseme"];
      }
    
    
      if (((MyPointAnnotation *)annotation).pointId == 1)
      {
          //the annotation with id 1.
      }
    return view;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-21
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      相关资源
      最近更新 更多