且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MKMapView:自定义视图而不是注释引脚

更新时间:2022-12-30 10:19:10

当您想将自己的图像用于注释视图时,你应该创建一个 MKAnnotationView 而不是 MKPinAnnotationView

When you want to use your own image for an annotation view, you should create an MKAnnotationView instead of an MKPinAnnotationView.

MKPinAnnotationView MKAnnotationView 的子类,因此它有一个图像属性,但它通常会覆盖它并绘制图像(这就是它的用途)。

MKPinAnnotationView is a subclass of MKAnnotationView so it has an image property but it generally overrides that and draws a pin image (that's what it's for).

所以将代码更改为:

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



请注意 animatesDrop 也被注释掉了,因为该属性只存在于 MKPinAnnotationView 中。


Notice that animatesDrop is also commented out since that property only exists in MKPinAnnotationView.

如果你还是想让你的图像注释掉落,你必须自己做动画。您可以搜索Stack Overflow以获取animatesdrop mkannotationview,您将找到几个答案。以下是前两个:

If you still want your image annotations to drop, you'll have to do the animation yourself. You can search Stack Overflow for "animatesdrop mkannotationview" and you'll find several answers. Here are the first two:

  • Is it possible to call animatesDrop in a MKAnnotationView rather than MKPinAnnotationView?
  • How can I create a custom "pin-drop" animation using MKAnnotationView?