在MKPointAnnotation上执行后端操作

时间:2012-05-10 08:27:46

标签: ios ios5 mkmapview mkannotation mkannotationview

我使用MKPointAnnotation在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]];
}

2 个答案:

答案 0 :(得分:3)

您还可以创建一个继承自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;
}

答案 1 :(得分:2)

似乎有两种方法。

如果一次只能选择一个注释,则可以访问封闭式-selectedAnnotations的{​​{1}}属性。

另一种方法是检查MKMapView中的sender,这是对触发操作的showOptions:的引用。找出其附件UIButton,它将为您提供相关的MKAnnotationView。然后,您可以将其作为ivar存储或(我的首选方法)使用某些运行时魔术 - 以-annotation的形式,在objc_setAssociatedObject()中声明 - 将注释的引用附加到操作表,允许在其委托回调中轻松检索。

(如果需要,您可以在按钮创建阶段实际执行此操作,并将注释的引用附加到UIButton,可以直接在<objc/runtime.h>中选取并重新附加到操作表。< / p>

showOptions:如果符合您的需要,我认为这是更容易的方法。