MKAnnotationView自定义图像未显示

时间:2012-08-06 14:09:42

标签: iphone objective-c cocoa-touch mkmapview mkannotationview

我正在尝试在地图上添加自定义图片而不是常规图钉。但它仍然是一个红色的针...我错过了什么?

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{  
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView *annView = [[MKPinAnnotationView alloc]     initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
    annView.animatesDrop = TRUE;
    annView.image = [UIImage imageNamed:@"CustomPin.png"];
    return annView;
}

3 个答案:

答案 0 :(得分:6)

MKMapView: Instead of Annotation Pin, a custom view

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation 
{
    MKAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) 
            pinView = [[MKAnnotationView alloc]
                                         initWithAnnotation:annotation reuseIdentifier:defaultPinID];

        //pinView.pinColor = MKPinAnnotationColorGreen; 
        pinView.canShowCallout = YES;
        //pinView.animatesDrop = YES;
        pinView.image = [UIImage imageNamed:@"pinks.jpg"];    //as suggested by Squatch
    } 
    else {
        [mapView.userLocation setTitle:@"I am here"];
    }
    return pinView;
}

答案 1 :(得分:3)

您只需从代码中删除一行... annView.animatesDrop = TRUE;

保留代码 -

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{  
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView *annView = [[MKPinAnnotationView alloc]     initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
    annView.image = [UIImage imageNamed:@"CustomPin.png"];
    return annView;
}  

答案 2 :(得分:0)

我发现查看Apples文档并下载示例代码很有帮助。

http://developer.apple.com/library/ios/#samplecode/MapCallouts/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009746

他们正在为他们的地图实施自定义注释。