iOS Mapkit 自定义标注

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7164469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:13:29  来源:igfitidea点击:

iOS Mapkit Custom Callout

iosmapkit

提问by Bahamut

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.

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

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

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?

任何想法如何在内部的 ui 控件中实现此自定义标注?

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

编辑:这是我遵循类似 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];    
}

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.

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

回答by Bahamut

Found out the answer after a few mins of nap.

小睡几分钟后找到答案。

using thisas 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.

其中显示了注释标注。再次感谢您的回答,安娜女士。