2013-03-11 133 views
5

我有一個地圖視圖控制器(UIViewController,MKMapView)及其委託(HCIResultMapViewController)。mkannotation中的自定義數據變量

我希望在這部分有以下功能。

1)。我希望用我的定製NSObject的,讓我能與像標題,副標題等基本實體沿着別人的信息關聯起來

因此,根據我的需要我編寫如下

在HCIResultMapViewController

- (void)viewDidLoad { 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 

_houseList = [[_resultList objectForKey:@"result"] objectForKey:@"listings"]; 

// NSLog([_houseList description]); 

int i = 0; 

for (NSDictionary *house in _houseList) { 

    HCIAnnotationViewController *annotation = [[HCIAnnotationViewController alloc] 
            initwithHouse:house]; 
    [_mapView addAnnotation:annotation]; 
    // NSLog(@"asdjhasdjsajdhaksdjghasdasdjahsdahskvdka"); 
    self.currIdentifier = i; 
    i++; 
} 

[_mapView setShowsUserLocation:NO]; 
} 

其他委託功能

-(void) mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { 

for (NSObject<MKAnnotation> *annotation in _mapView.selectedAnnotations) { 
    NSLog(@"hellomaster"); 
    NSLog(annotation.title); 
    if ([annotation isKindOfClass:[HCIAnnotationViewController class]]) { 
     NSLog(@"hellomaster"); 
    } 
} 

最後一個

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

NSString *identifier = @"currIdentifier"; 

    MKPinAnnotationView *annotationView = 
    (MKPinAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 

    if (annotationView == nil) { 
     annotationView = [[MKPinAnnotationView alloc] 
          initWithAnnotation:annotation 
          reuseIdentifier:identifier]; 
    } else { 
     annotationView.annotation = annotation; 
    } 

    annotationView.enabled = YES; 
    annotationView.canShowCallout = YES; 
annotationView.tag = self.currIdentifier; 

    // Create a UIButton object to add on the 
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [rightButton setTitle:annotation.title forState:UIControlStateNormal]; 
    [annotationView setRightCalloutAccessoryView:rightButton]; 

/* 
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
    [leftButton setTitle:annotation.title forState:UIControlStateNormal]; 
    [annotationView setLeftCalloutAccessoryView:leftButton]; 
    */ 
    return annotationView; 
} 

但我看到類等價失敗。你能告訴我我做錯了什麼嗎?

我想我想要的,簡單來說,我怎麼能發送一些數據(NSDictionary *)以及註釋,以便我可以隨時檢索它?

請不要標記爲重複的問題左右。我嘗試了很多問題,谷歌搜索等,但找不到合適的解決方案。

+0

你想創建自定義註釋..例如標題,副標題? – iPatel 2013-03-11 12:24:38

+0

沒有。我希望我的註釋包含一個NSDictionary *以及諸如標題,副標題之類的實體。 – 2013-03-11 12:26:38

回答

1

我找不到實現精確的方式自定義數據變量或我的類等價失敗的原因。但我想出了一個辦法來解決這種情況。這可能在方法上是多餘的,但仍然像魅力一樣起作用。

所以這個想法是在控制按鈕中使用標記系統。我觀察到,每次我向我的地圖視圖發送消息以添加註釋時,都會立即調用我的viewforannotation方法。由於我遍歷一個數組,我維護了一個索引全局指針,用它來設置控件按鈕的標籤。稍後當按鈕被點擊時,我檢索標籤並使用它來獲取我想要的對象。

其他人有任何替代或直接的解決方案,請張貼。

1

在這裏您還可以設置NSMutableDictionary instan的NSString
創建自定義AnnotationView

#import <MapKit/MapKit.h> 

@interface AnnotationView : MKPlacemark 

@property (nonatomic, readwrite, assign) CLLocationCoordinate2D coordinate; 

@property (nonatomic, strong) NSString *title; //Here You cam set `NSMutableDictionary` instand of `NSString` 
@property (nonatomic, strong) NSString *subtitle; //Here You cam set `NSMutableDictionary` instand of `NSString` 


@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

你是否建議我使用自己有一個元素(NSDictionary *)的自定義註釋視圖?但這並不能解釋爲什麼我的自定義註釋在類對等上失敗。你能解釋一下嗎? – 2013-03-11 12:35:28