• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 向 MKMapView 注释添加操作

[复制链接]
菜鸟教程小白 发表于 2022-12-12 18:33:37 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个带有一堆注释的 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 *)mapViewMKMapView *)mapView viewForAnnotationid <MKAnnotation>)annotation
{
    MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;

    NSLog(@"in Created");

    return annotationView;
}

- (void)mapViewMKMapView *)mapView didSelectAnnotationViewMKAnnotationView *)view
{

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

    NSLog(@"Selected");
}



Best Answer-推荐答案


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

- (MKAnnotationView *)mapViewMKMapView *)mapView viewForAnnotationid <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 actionselector(openDetail forControlEvents:UIControlEventTouchUpInside];
        [accessory setFrame:CGRectMake(0, 0, 30, 30)];
        [annView setRightCalloutAccessoryView:accessory];
    }
    [annView setEnabled:YES];
    [annView setCanShowCallout:YES];
    return annView;
}

关于ios - 向 MKMapView 注释添加操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21594057/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap