iOS Mapkit 自定义标注

发布于 2024-12-01 07:04:22 字数 2739 浏览 1 评论 0原文

这是一个示例自定义标注,我想要获得类似的样式。我正在考虑继承 MKAnnotation 但我不知道如何启动它,而且我不知道标注的设计是否可以被覆盖。

http://1.bp.blogspot.com/_xfo3fwc97Fg/TJ9RZrHVOaI/AAAAAAAAAU4/buhP3q3y0G4/s1600/fmip_locate_20100622.png

任意如何使用 ui 控件实现此自定义标注的想法 里面?

编辑:这是我的代码,来自类似的 StackOverflow 答案:

- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    if(annotationView)
        return annotationView;
    else
    {
        UIImage *img = [UIImage imageNamed:@"default_thumb.png"];

        MKAnnotationView *annotationView = 
            [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
        annotationView.canShowCallout = YES;
        annotationView.image = img;
        annotationView.draggable = YES;

        /*
        // change left accessory view button with image
        UIImageView *leftAccView = [[UIImageView alloc] initWithImage:img];
        annotationView.leftCalloutAccessoryView = leftAccView;

        //include a right button to show more info         
        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton addTarget:self action:@selector(calloutSubMenu:) forControlEvents:UIControlEventTouchUpInside];
        [rightButton setTitle:annotation.title forState:UIControlStateNormal];
        annotationView.rightCalloutAccessoryView = rightButton;         
         */

        return annotationView;
    }
    return nil;
}

// Customize accessory view
- (void)mapView:(MKMapView *)mapview annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [mapview deselectAnnotation:view.annotation animated:YES];

    PopOverCallout *popOverCallout = [[PopOverCallout alloc]init];

    UIPopoverController *popOver = [[UIPopoverController alloc] initWithContentViewController:popOverCallout];

    self.annotationPopoverController = popOver;

    popOver.popoverContentSize = CGSizeMake(500, 500);

    [popOver presentPopoverFromRect:view.bounds inView:view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];    
}

(void)mapView:(MKMapView *)mapview annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

每当触摸 pin 时都应该调用以显示标注,对吗 但发生的情况是显示空白的常规标注。我可能做错了什么?

顺便说一句:UIViewController 子类在它的 XIB 上只有一个标签,几乎是空白的。

Here's a sample custom callout that I'd want to get a similar style of. I was thinking of inheriting from MKAnnotation but I'm lost how to start it and I don't know if the callout's design could be overridden.

http://1.bp.blogspot.com/_xfo3fwc97Fg/TJ9RZrHVOaI/AAAAAAAAAU4/buhP3q3y0G4/s1600/fmip_locate_20100622.png

Any ideas how to implement this custom callout with ui controls inside?

EDIT: Here's my code from following the similar StackOverflow answer:

- (MKAnnotationView *)mapView:(MKMapView *)mapview viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
    if(annotationView)
        return annotationView;
    else
    {
        UIImage *img = [UIImage imageNamed:@"default_thumb.png"];

        MKAnnotationView *annotationView = 
            [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
        annotationView.canShowCallout = YES;
        annotationView.image = img;
        annotationView.draggable = YES;

        /*
        // change left accessory view button with image
        UIImageView *leftAccView = [[UIImageView alloc] initWithImage:img];
        annotationView.leftCalloutAccessoryView = leftAccView;

        //include a right button to show more info         
        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton addTarget:self action:@selector(calloutSubMenu:) forControlEvents:UIControlEventTouchUpInside];
        [rightButton setTitle:annotation.title forState:UIControlStateNormal];
        annotationView.rightCalloutAccessoryView = rightButton;         
         */

        return annotationView;
    }
    return nil;
}

// Customize accessory view
- (void)mapView:(MKMapView *)mapview annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [mapview deselectAnnotation:view.annotation animated:YES];

    PopOverCallout *popOverCallout = [[PopOverCallout alloc]init];

    UIPopoverController *popOver = [[UIPopoverController alloc] initWithContentViewController:popOverCallout];

    self.annotationPopoverController = popOver;

    popOver.popoverContentSize = CGSizeMake(500, 500);

    [popOver presentPopoverFromRect:view.bounds inView:view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];    
}

The

(void)mapView:(MKMapView *)mapview annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

should be called whenever the pin is touched to show the callout right? but what happens is that a blank regular callout is shown. What might I be doing wrong?

BTW: the UIViewController subclass only has a label on it's XIB and is pretty much blank.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你げ笑在眉眼 2024-12-08 07:04:22

小睡几分钟后找到了答案。

使用 this 作为指南,我只需要覆盖

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

它显示注释标注。再次感谢安娜女士的回答。

Found out the answer after a few mins of nap.

using this as the guide, I just needed to override

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

which shows the annotation callout. Thanks again for the answer, Ms. Anna.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文