将其添加到地图时使用自定义注释

在将其添加到mapview时,我无法访问自定义注记类。 我有自定义类正常工作,但我当我把它添加到地图我不知道如何通过这个委托访问自定义注释:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 

我试过在网上找,没有find任何东西。 任何帮助将是伟大的。

它被这样调用:

 CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(shops.latitude, shops.longitude); AnnotationForId *shop = [[AnnotationForId alloc] initWithCoordinate:coords]; //[[CLLocationCoordinate2DMake(shops.latitude, shops.longtitude)]]; shop.title = shops.name; shop.subtitle = @"Coffee Shop"; shop.shopId = shops.id; [map addAnnotation:shop]; 

这是如何创build自定义AnnotationView的简单示例。

创build自定义AnnotationView

 #import <MapKit/MapKit.h> @interface AnnotationView : MKPlacemark @property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate; @property (nonatomic, strong) NSString *title; @property (nonatomic, strong) NSString *subtitle; // you can put here any controllers that you want. (such like UIImage, UIView,...etc) @end 

.m file

 #import "AnnotationView.h" @implementation AnnotationView - (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary { if ((self = [super initWithCoordinate:coordinate addressDictionary:addressDictionary])) { self.coordinate = coordinate; } return self; } @end 

//使用Annotation在相关的.m file添加#import "AnnotationView.h"

 CLLocationCoordinate2D pCoordinate ; pCoordinate.latitude = LatValue; pCoordinate.longitude = LanValue; // Create Obj Of AnnotationView class AnnotationView *annotation = [[AnnotationView alloc] initWithCoordinate:pCoordinate addressDictionary:nil] ; annotation.title = @"I m Here"; annotation.subtitle = @"This is Sub Tiitle"; [self.mapView addAnnotation:annotation]; 

testing注释以查看它是否与您的自定义类相同:

 - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { MKAnnotationView *mkav = nil; if ([annotation isKindOfClass:[AnnotationForId class]]) { // This should be safe now. AnnotationForId *aid = annotation; // Whatever you wanted to add, including making your own view. } return mkav; }