基于我的不同枚举值,我需要在我的 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/