代码之家  ›  专栏  ›  技术社区  ›  Armando

MKPinAnnotationView未设置自定义图像

  •  0
  • Armando  · 技术社区  · 9 年前

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

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

    }

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

    2 回复  |  直到 9 年前
        1
  •  4
  •   Vishnu gondlekar    9 年前

    而不是 MKPinAnnotationView 你能用吗 MKAnnotationView ? 你必须使用follow而不是 MKPin注释视图

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

    建议使用iOS 9.0以后的版本 MK注释视图 。这应该能解决你的问题。

        2
  •  0
  •   user3069232    7 年前

    这是iOS 11.x Swift 4中的样子,请注意,如果你犯了与我相同的错误,这将不起作用 MKPin注释视图 。这将制作一个别针,您将无法更改它后面的图像。

    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;
    }
    
    推荐文章