2013-03-11 73 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