【问题标题】:Adding action to MKMapView annotation向 MKMapView 注释添加操作
【发布时间】:2023-09-27 13:27:01
【问题描述】:

我有一个带有一堆注释的 MKMapView,但我无法向它们添加操作。我只是在创建一个 MKPointAnnotation,但我无法为其添加操作。每当我单击注释时,什么都不会发生。这是我设置注释的方式-

CLLocationCoordinate2D monteVista;
monteVista.latitude = (double) 37.83029;
monteVista.longitude = (double) -121.98827;

MKPointAnnotation *monteVistaPoint = [[MKPointAnnotation alloc] init];
monteVistaPoint.coordinate = monteVista;
monteVistaPoint.title = @"Monte Vista";

CLLocationCoordinate2D sanRamonValley;
sanRamonValley.latitude = (double) 37.82609;
sanRamonValley.longitude = (double) -122.00603;

MKPointAnnotation *sanRamonValleyPoint = [[MKPointAnnotation alloc] init];
sanRamonValleyPoint.coordinate = sanRamonValley;
sanRamonValleyPoint.title = @"San Ramon Valley";

CLLocationCoordinate2D doughertyValley;
doughertyValley.latitude = (double) 37.76845;
doughertyValley.longitude = (double) -121.90342;

MKPointAnnotation *doughertyValleyPoint = [[MKPointAnnotation alloc] init];
doughertyValleyPoint.coordinate = doughertyValley;
doughertyValleyPoint.title = @"Dougherty Valley";


NSArray *points = [[NSArray alloc] initWithObjects: monteVistaPoint, sanRamonValleyPoint, doughertyValleyPoint, nil];
[self.schoolMap addAnnotations:points];
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;

    NSLog(@"Pin Created");

    return annotationView;
}

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{

    MKPointAnnotation *testPoint = [[MKPointAnnotation alloc] init];
    testPoint = view.annotation;
    self.testText.text = testPoint.title;

    NSLog(@"Selected");
}

【问题讨论】:

  • 你收到委托didSelectAnnotationView了吗?
  • 参考这个希望它会起作用 [link][1] [1]: *.com/questions/6784936/…
  • 您想对被点击的注释本身做出响应,还是想在其标注上添加一个按钮并做出响应?在 didSelectAnnotationView 中,不要创建注释的 new 实例(这就是 alloc+init 所做的),然后您只需用另一个实例覆盖它。只需执行MKPointAnnotation *testPoint = view.annotation;。请记住,要调用委托方法,必须设置地图视图的delegate(或在 xib/storyboard 中连接)。对于标注按钮选项,请参阅 jai 给出的链接。我不推荐答案中的自定义方法和标签方法。

标签: ios mkmapview mkannotationview mkpointannotation


【解决方案1】:

您需要更改viewForAnnotation: 方法才能自定义,然后只需实现openDetail: action 方法

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if (annotation == mapView.userLocation)
    {
        return nil;
    }

    static NSString *identifier = @"CustomAnnotation";
    MKAnnotationView* annView = nil;
    if ([annotation isKindOfClass:[CustomAnnotation class]]) {

        annView = (MKAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annView == nil) {
            annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        } else {
            annView.annotation = annotation;
        }

        UIImage *image = nil;
        image = [UIImage imageNamed:@"pin2"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        [annView addSubview:imageView];
        [annView setFrame:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
        //        [annView setBackgroundColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:0.1]];
        UIButton*accessory = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [accessory setTag:[(CustomAnnotation*)annotation tag]];
        [accessory addTarget:self action:@selector(openDetail:) forControlEvents:UIControlEventTouchUpInside];
        [accessory setFrame:CGRectMake(0, 0, 30, 30)];
        [annView setRightCalloutAccessoryView:accessory];
    }
    [annView setEnabled:YES];
    [annView setCanShowCallout:YES];
    return annView;
}

【讨论】: