ios - 在 mkMapview 中显示注释的不同图像

标签 ios mkmapview

基于我的不同枚举值,我需要在我的 map View 中显示不同的图像。 我该怎么做?我是 iOS 开发的新手。请任何人帮助我。

最佳答案

您需要在 View Controller 中使用“ViewForAnnotation”委托(delegate)方法。

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation
{
    if( [annotation isKindOfClass:[Annotation class] ] )
    {

        static NSString * AnnotationID = @"Annotation";

        Annotation * pannotation = (Annotation *)annotation;
        //if( pannotation == nil ) return nil;

        MKAnnotationView *annotationView = nil;

        annotationView = [self.mMapView  dequeueReusableAnnotationViewWithIdentifier:AnnotationID];
        if( annotationView == nil )
        {           
            annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
                                                              reuseIdentifier:AnnotationID] autorelease];

        }   


    UIImage * flagImage = nil;

    if(Your enum Values)
    flagImage = [UIImage imageNamed:@"darkgreendot.png"];
    else if(....)
    flagImage = [UIImage imageNamed:@"orangedot.png"];
    else
    flagImage = [UIImage imageNamed:@"bluedot.png"];

    CGRect resizeRect;  
    resizeRect.size = flagImage.size;   
    resizeRect.origin = (CGPoint){0.0f, 0.0f};
    UIGraphicsBeginImageContext(resizeRect.size);
    [flagImage drawInRect:resizeRect];
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    
    annotationView.image = resizedImage;

    return annotationView;
    }
  }

希望对您有所帮助。

关于ios - 在 mkMapview 中显示注释的不同图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23009193/

相关文章:

ios - 创建 postDict 后如何从 Firebase 快照中检索和使用数据;我得到 "0\0\0"

iphone - 在单例中设置字典导致 EXC_BAD_ACCESS

ios - 在 ios Objective-c 的 mapview 上添加带有多个图像的多个注释?

ios - UITapGestureRecognizer 未正确触发

ios - 在pubspec.yaml中添加Google Maps软件包后,Flutter的iOS应用将无法构建

ios - 如何在 Swift 中使用 MapKit 将叠加层粘贴到用户位置

ios - MKMapView 中心不正确

swiftui - 如何为 SwiftUI ViewRepresentable 自定义 MKMapView 委托(delegate)操作?

ios - 如何获取谷歌地图标记的 UIView 实例?

ios - 有没有 `addObject` 的被动方法?