如何在MKMapView中的MKAnnotation上设置图像 [英] How to set image on MKAnnotation in MKMapView

查看:87
本文介绍了如何在MKMapView中的MKAnnotation上设置图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个用于聊天的应用程序,我必须在地图上显示所有朋友的图像。
请提供实施指南。

I am developing an app for chating, I have to display all friends on a Map with their image. Please provide guidance to implement it.

我使用了以下代码......

I have used following code...

- (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的对象,它用于显示引脚注释。

What you are doing wrong is you are returning object of class MKPinAnnotationView, which is used for displaying pin-annotation.

你应该使用MKAnnotationView类的对象。然后,通过更改其图像来自定义它:

You should use object of MKAnnotationView class. Then, customise it by changing its image:

annotationView.image = [UIImage imageNamed:@"friend_image.png"];

现在,您可以获取朋友的照片而不是默认的图片。

Now, you can get your friend's photo instead of default pin image.

以下是解决问题的完整代码:

Below is full code solving your problem:

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

这篇关于如何在MKMapView中的MKAnnotation上设置图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆