对 MKPoint 注释执行后端操作



我已经使用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]];
}

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

似乎有两种方法。

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

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

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

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