我正在开发一个聊天应用程序,我必须在地图上显示所有朋友及其图像。
请提供实施指导。
我使用了以下代码...
- (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;
}
Thanks
您做错的事情是您返回 MKPinAnnotationView 类的对象,该对象用于显示引脚注释。
您应该使用 MKAnnotationView 类的对象。然后,通过更改其图像来自定义它:
annotationView.image = [UIImage imageNamed:@"friend_image.png"];
现在,您可以获取朋友的照片,而不是默认的图钉图像。
下面是解决您的问题的完整代码:
- (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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)