9

我是objective-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;
}
4

4 回答 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];
}
于 2012-06-05T11:37:47.303 回答
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;

}
于 2013-04-19T13:47:13.963 回答
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
}
于 2015-11-13T23:56:41.660 回答
-3

您必须自己实现标注视图,并使用

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

这个委托使标注视图显示在正确的位置,

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

于 2013-10-22T11:22:45.940 回答