3

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

- (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;
}
4

3 回答 3

6

MKMapView:自定义视图代替 Annotation Pin

-(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;
}
于 2012-08-06T14:23:22.490 回答
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;
}  
于 2012-08-06T14:42:44.787 回答
0

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

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

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

于 2012-08-06T14:20:48.400 回答