我正在开发一个聊天应用程序,我必须在 map 上显示所有 friend 的图像。 请提供实现指南。
我使用了以下代码...
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *annView = [[MKPinAnnotationView alloc]init];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Done.png"]];
annView.animatesDrop = TRUE;
annView.canShowCallout = YES;
annView.calloutOffset = CGPointMake(-5, 5);
[annView addSubview:imageView];
return annView;
}
谢谢
最佳答案
你做错的是你返回了类 MKPinAnnotationView 的对象,它用于显示 pin-annotation。
您应该使用 MKAnnotationView 类的对象。然后,通过更改其图像对其进行自定义:
annotationView.image = [UIImage imageNamed:@"friend_image.png"];
现在,您可以获得 friend 的照片,而不是默认的 pin 图片。
下面是解决您问题的完整代码:
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
static NSString *annotationViewReuseIdentifier = @"annotationViewReuseIdentifier";
MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifier];
if (annotationView == nil)
{
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifier] autorelease];
}
// here you can assign your friend's image
annotationView.image = [UIImage imageNamed:@"friend_image.png"];
annotationView.annotation = annotation;
// add below line of code to enable selection on annotation view
annotationView.canShowCallout = YES
return annotationView;
}
关于ios - 如何在 MKMapView 的 MKAnnotation 上设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24527912/