2013-04-20 40 views
0

您好我有一個NSMutableArray裏與註釋對象的follwoing代碼將不添加註釋:IOS:addAnnotations到MapView的工作不

在.H我

 @property (retain, nonatomic) MarketsDataController *marketList; 

在.M我已經(添加註釋不起作用什麼也不顯示地圖)

_marketList = [_marketService.marketsDataController retain]; 
    NSLog(@"%@!!!", [_marketList objectInListAtIndex:0].title); 
    [mapView addAnnotation:[_marketList objectInListAtIndex:0]]; 
    [mapView addAnnotation:[_marketList objectInListAtIndex:1]]; 
    [mapView addAnnotation:[_marketList objectInListAtIndex:2]]; 

MarketsListDataController看起來是這樣的:

#import "MarketsDataController.h" 

    //The following interface is used for private methods 
    @interface MarketsDataController() 
    - (void)initializeMarketsList; 
    @end 

    @implementation MarketsDataController 

    //Set initial values for instance variables 
    - (id)init { 
     NSLog(@"init MarketsDataController"); 
     if (self = [super init]) { 
      [self initializeMarketsList]; 
      return self; 
     } 
     return nil; 
    } 

    - (void)initializeMarketsList{ 
     NSMutableArray *marketsList = [[NSMutableArray alloc] init]; //Initialize the product list 
     _marketsList = marketsList;      //Set the markets list to the markets list 
    } 

    //Return the number of products 
    - (NSUInteger)countOfList { 
     return [_marketsList count]; 
    } 

    //Return a product within the list 
    - (MapAnnotation *)objectInListAtIndex:(NSUInteger)theIndex { 
     return [_marketsList objectAtIndex:theIndex]; 
    } 

    - (void)addAnnotationToList:(MapAnnotation *)mapAnnotation{ 
     NSLog(@"Adding market"); 
     [_marketsList addObject:mapAnnotation]; 
    } 

    - (void)dealloc { 
     NSLog(@"DEALLOC MarketsDataController"); 
     [_marketsList release]; 
     [super dealloc]; 

    } 

    @end 

MapAnnotation .M樣子:

#import "MapAnnotation.h" 

@implementation MapAnnotation 

@synthesize coordinate, title, subtitle; 

- (id)init{ 
    CLLocationCoordinate2D location; 
    location.latitude = 0; 
    location.longitude = 0; 
    return [self initWithCoordinate:coordinate title:nil subtitle:nil]; 
} 

- (id)initWithCoordinate:(CLLocationCoordinate2D) c title:(NSString *)t subtitle:(NSString *)st{ 
    self = [super init]; 
    coordinate = c; 
    title = [t retain]; 
    subtitle = [st retain]; 
    return self; 
} 

- (void) dealloc{ 
    [title release]; 
    [subtitle release]; 
    [super dealloc]; 
} 

@end 

我創建它們並將它們添加另一個類,如:

if (![latitude isEqual:[NSNull null]] && ![longitude isEqual:[NSNull null]]) { 
        NSLog(@"%d", i); 
        NSLog(@"%@", title); 
        CLLocationCoordinate2D coordinate; 
        coordinate.longitude = [latitude doubleValue]; 
        coordinate.longitude = [longitude doubleValue]; 
        [self buildMarketsList:coordinate title:title subtitle:@""]; //build the browse list product 

       } 

方法如下:

- (void)buildMarketsList:(CLLocationCoordinate2D)c title:(NSString *)t subtitle:(NSString *)st{ 
    MapAnnotation *mapAnnotation = [[MapAnnotation alloc]initWithCoordinate:c title:t subtitle:st]; 
    [_marketsDataController addAnnotationToList:mapAnnotation]; 
    [mapAnnotation release]; 
} 

哪有我添加一個註釋對象數組,實現< MKAnnotation>?我沒有收到任何錯誤,也沒有註釋顯示。

+0

_marketsList是類型的NSMutableArray的?這將如何工作_marketList.marketsList?還有_前綴的變量被建議只在setter中使用。 – Anupdas 2013-04-20 06:16:27

+0

爲什麼你保留_marketList.marketsList? – Balu 2013-04-20 06:16:35

+0

_marketsList是一個擁有保存註釋對象的NSMutable數組的對象。 – wwjdm 2013-04-20 06:46:48

回答

2

發現問題:

變化:

   coordinate.longitude = [latitude doubleValue]; 
       coordinate.longitude = [longitude doubleValue]; 

要:

   coordinate.latitude = [latitude doubleValue]; 
       coordinate.longitude = [longitude doubleValue]; 
+0

我剛剛看到這個,並認爲..大聲笑,多麼愚蠢的錯誤,我敢肯定這不會是我的原因.. *檢查*,它是...... *臉掌*乾杯埃爾:) – GONeale 2015-01-19 12:53:30

0

_marketsList不是具有數組的對象。它的數組。

如果你有一個名爲marketsList屬性,該屬性稱爲_marketsList

@property (nonatomic, retain) NSMutableArray * marketsList; //in your .h 


@synthesize marketsList = _marketsList; //in your .m 

變量的支持,那麼你應該做這樣的事情:

- (void)initializeMarketsList{ 
    marketsList = [[NSMutableArray alloc] init]; //Initialize the product list 

    [marketsList addObject:myAnnotation];//ad some annotations 
} 

,然後你的註解陣列添加到地圖查看

[mapView addAnnotations:marketsList]; 
+0

我編輯了我的問題。看原來的鍋。 – wwjdm 2013-04-20 16:53:05

+0

我試過這個,它沒有工作 – wwjdm 2013-04-21 15:55:47