2013-03-28 131 views
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]; 

回答

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

但是如何在委託方法中調用該自定義註釋?我有一個自定義註釋設置,我只是不能訪問它在代表方法 – user1179321

+0

你想要什麼...把這個代碼,並..按照它的建議,那麼它會工作得很好,並從委託方法alos這個調用太 – iPatel

+0

我已經添加一個id到我的自定義註釋中,我想在委託方法中訪問它,當我調用annotation.id時它不起作用 – user1179321

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; 
} 
相關問題