【问题标题】:MKPinAnnotationView doesn't set custom imageMKPinAnnotationView 没有设置自定义图像
【发布时间】:2016-02-11 15:06:38
【问题描述】:

我必须为不同的引脚设置自定义图像。使用我的代码,我可以为它们中的每一个设置自定义颜色,但是当我尝试设置自定义图像而不是颜色时,它不起作用。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(MyAnnotation *)annotation {

static NSString *identifier = @"MyLocation";

if ([annotation isKindOfClass:[MyAnnotation class]]) {

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.myMap dequeueReusableAnnotationViewWithIdentifier:identifier];

    if (annotationView == nil) {

        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];

    } else {

        annotationView.annotation = annotation;

    }

    annotationView.enabled = YES;
    annotationView.canShowCallout = NO;

    NSString *stringType = [NSString stringWithFormat:@"%@", [(MyAnnotation *)annotationView.annotation stringType]];

    if ([stringType isEqualToString:@"0"]) {

        annotationView.image = [UIImage imageNamed:@"IconUserCerco.png"];
        //annotationView.pinColor = MKPinAnnotationColorGreen;

    } else if ([stringType isEqualToString:@"1"]) {

        annotationView.image = [UIImage imageNamed:@"IconUserDefault.png"];
        //annotationView.pinColor = MKPinAnnotationColorPurple;

    } else if ([stringType isEqualToString:@"2"]) {

        annotationView.image = [UIImage imageNamed:@"PinUniversity.png"];
        //annotationView.pinColor = MKPinAnnotationColorRed;

    }

    return annotationView;

}

return nil;

}

任何帮助将不胜感激。提前致谢。

【问题讨论】:

标签: ios xcode mkmapview mkpinannotationview


【解决方案1】:

你能用MKAnnotationView代替MKPinAnnotationView吗?您必须使用以下而不是 MKPinAnnotationView

MKAnnotationView *annotationView = [self.myMap dequeueReusableAnnotationViewWithIdentifier:identifier];

iOS 9.0 以上推荐使用MKAnnotationView。这应该可以解决您的问题。

【讨论】:

    【解决方案2】:

    这就是它在 iOS 11.x Swift 4 中的样子,请注意,如果你使用 MKPinAnnotationView 犯了和我一样的错误,这将不起作用。这将制作一个图钉,您将无法更改其背后的图像。

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    
        if annotation is MKUserLocation  {
            return nil
        }
    
        let reuseId = "pin"
        var pav:MKAnnotationView?
        if (pav == nil)
        {
            pav = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pav?.isDraggable = true
            pav?.canShowCallout = true
            pav?.image = UIImage(named: "WIP.png")
            pav?.calloutOffset = CGPoint(x: -8, y: 0)
            pav?.autoresizesSubviews = true
    //      pav?.rightCalloutAccessoryView = UIButton(type: .roundedRect)
            pav?.leftCalloutAccessoryView = UIButton(type: .contactAdd)
        }
        else
        {
            pav?.annotation = annotation;
        }
    
        return pav;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多