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

时间:2013-03-28 14:12:02

标签: iphone ios objective-c mapkit

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

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

我试过在线查找并没有找到任何东西。任何帮助都会很棒。

它的召唤如下:

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];

2 个答案:

答案 0 :(得分:1)

这是关于如何创建自定义AnnotationView的简单示例。

创建自定义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

//使用注释在相关的#import "AnnotationView.h"

中添加.m file
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];

答案 1 :(得分:0)

测试注释以查看它是否与自定义类相同:

- (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;
}
相关问题