如何添加在图形页面定制标注视图

问题描述 投票:9回答:4

我是新目标C到mapkit。我可以添加自定义的注释在图形页面。

我需要把类似下面的图像的自定义标注视图

.

但我没有得到我怎么能设计出这样的标注视图。

我知道我需要考虑的标注方法添加标注。

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *AnnotationViewID = @"annotationViewID";

    MKAnnotationView *annotationView = (MKAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil)
    {
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
    }

    annotationView.image = [UIImage imageNamed:@"blue_without_pin.png"];
    annotationView.annotation = annotation;


    return annotationView;
}
iphone objective-c mkmapview mkannotationview
4个回答
0
投票

基本上你想要做的是通过实现你的MKMapViewDelegate例如下面的方法添加一个新的自定义视图:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    CGPoint annotationCenter = [mapView convertCoordinate:view.annotation.coordinate toPointToView:mapView];
    [self displayCustomCalloutForAnnotation:view.annotation center:annotationCenter];
}

0
投票
@interface CustomAnnotaionView : MKAnnotationView




@property (strong, nonatomic) UIView *calloutView;




-(void)setSelected:(BOOL)selected animated:(BOOL)animated;




@end




@implementation CustomAnnotaionView




@synthesize calloutView = _calloutView;




-(void)setSelected:(BOOL)selected animated:(BOOL)animated





{

    [super setSelected:selected animated:animated];

    if(selected)
    {

        [self.calloutView setFrame:CGRectMake(30, 130, 250, 135)];
        [self.calloutView sizeToFit];
        self.calloutView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"popup.png"]];
        [self addSubview:self.calloutView];

    }
    else
    {

        [self.calloutView removeFromSuperview];
    }

}

-(void)didAddSubview:(UIView *)subview
{

    if([[[subview class]description]isEqualToString:@"UICalloutView"])
    {

        [subview removeFromSuperview];

    }
}

Use this customAnnotationView class in MapView delegate method,

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id 

<MKAnnotation>)annotation

{



    if([annotation isKindOfClass:[MapAnnotation class]])
    {
       CustomAnnotationView *annotationView = (CustomAnnotaionView*)[theMapView dequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotationView"];
        if(!annotationView)
        {
         MapAnnotation *annotation = (MapAnnotation *)annotation;

         annotationView = [[CustomAnnotaionView alloc]initWithAnnotation:annotation reuseIdentifier:@"CustomAnnotationView"];

            [annotationView setCanShowCallout:YES];
        }
        else
            annotationView.annotation = annotation;

        return annotationView;

    }
    return nil;

}

0
投票

对此的一个解决方案是将检测注释选择相对于亲本的UIView的确切坐标,然后直接在的MKMapView的顶部绘制一个UIView。

这里有一个片段,它可以帮助:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    CGPoint pointInMap = [self.mapView convertCoordinate:buildingAnnotation.coordinate
                                           toPointToView:self.view];

    // Hide the default callout generated by MKMapView

    [mapView deselectAnnotation:view.annotation animated:NO];

    // Create a custom callout view and center the arrow on the pointInMap CGPoint
}

-3
投票

您必须实现标注看待自己,并用

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

此委托进行标注视图显示在正确的地方,

IOS没有自定义标注视图的任何方法还

© www.soinside.com 2019 - 2024. All rights reserved.