在 UIMapView 中放置图钉和图钉颜色失败

Posted

技术标签:

【中文标题】在 UIMapView 中放置图钉和图钉颜色失败【英文标题】:Drop pin and pin color failed in UIMapView 【发布时间】:2011-12-09 20:23:13 【问题描述】:

我在 MapView 上遇到了一点问题。

首先,无论我如何设置图钉的颜色,它都是红色的。

其次,drop pin没有动画,pin只是和UIView一起出现

这是我的代码。

非常感谢。

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 

    self->mapView.mapType = MKMapTypeHybrid;
    CLLocationCoordinate2D center;
    center.latitude = 33.79518;
    center.longitude = 130.92263;

    //declare span of map (height and width in degrees)
    MKCoordinateSpan span;
    span.latitudeDelta = .05;
    span.longitudeDelta = .01;

    //add center and span to a region, 
    //adjust the region to fit in the mapview 
    //and assign to mapview region
    MKCoordinateRegion region;
    region.center = center;
    region.span = span;
    mapView.region = [mapView regionThatFits:region];
    transportfu *addAnnotation = [[transportfu alloc] initWithCoordinate:center];
    [addAnnotation setTitle:@"City"];  
    [addAnnotation setSubTitle:@"Fukuoka"];  

    [mapView addAnnotation:addAnnotation];  
    [super viewDidLoad];
    mapView.showsUserLocation = YES;


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation  
    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MyPin"];  

    annView.canShowCallout = YES;  
    [annView setSelected:YES];  
    annView.pinColor = MKPinAnnotationColorGreen;  
    annView.calloutOffset = CGPointMake(-5, 5);  
    annView.animatesDrop=YES;  
    return annView;       
  

【问题讨论】:

【参考方案1】:

您的viewForAnnotation 方法需要一个出列方法。

试试这个:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
  
    static NSString *MyAnnotationIdentifier = @"MyPin";
    MKPinAnnotationView *annView =
    (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:MyAnnotationIdentifier];
    if (!annView)
    
        annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MyPin"];  

        annView.canShowCallout = YES;  
        [annView setSelected:YES];  
        annView.pinColor = MKPinAnnotationColorGreen;  
        annView.calloutOffset = CGPointMake(-5, 5);  
        annView.animatesDrop=YES;  
     else 
        annView.annotation = annotation;
    
    return annView;       
  

【讨论】:

以上是关于在 UIMapView 中放置图钉和图钉颜色失败的主要内容,如果未能解决你的问题,请参考以下文章

更改地图图钉(颜色或图像)

Xamarin iOS 更改某些地图图钉的颜色

掉落的图钉的动画不起作用

无法在 MapView,Swift 3 中设置自定义图钉图像

MKMapView:用户位置图钉颜色自动变为灰色

谷歌地图图标(图钉) - 如何定义颜色?