0

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

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

2 回答 2

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];
于 2013-03-28T14:23:01.823 回答
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;
}
于 2013-04-02T19:46:32.573 回答