2009-09-29 59 views
2

我使用下面的循環來填充我的MapView。然而,無論我做了多少次迭代,它總是一次只顯示一個引腳。MKMapView僅在n次迭代後顯示單個註釋

單獨宣佈項目似乎也沒有影響。

我在osx 10.5.8上使用了xCode 3.1.3的初始3.0 SDK,3.1 SDK更新日誌沒有提及MKMapKit框架的任何修復,所以我沒有覺得需要下載2.5GB文件。

for(NSDictionary* dict in results){ 
    NSLog(@"Made Annotation %@ at N%f E%f", [dict valueForKey:@"location"],[dict valueForKey:@"latitude"],[dict valueForKey:@"longitude"]); 
    NSLog(@"List of keys %@", dict); 

    LTAnnotation* pin = [[LTAnnotation alloc] initWithTitle: [dict valueForKey:@"location"] 
            latitude: [dict objectForKey:@"latitude"] 
            longitude: [dict objectForKey:@"longitude"] 
    ]; 

    [MapView addAnnotation: pin]; 

} 

這是第一個記錄的語句

Made Annotation London at N51.3 E0.07000000000000001 
Made Annotation Amsterdam at N52.22 E4.53 

輸出和第二是字典

List of keys { 
    id = 0; 
    latitude = 51.3; 
    location = London; 
    longitude = 0.07000000000000001; 
    time = "12:00-13:00"; 
} 
List of keys { 
    id = 1; 
    latitude = 52.22; 
    location = Amsterdam; 
    longitude = 4.53; 
    time = "12:00-13:00"; 
} 

的結構,如果您有興趣,這裏是我實現LTAnnotation

@interface LTAnnotation(Private) 
    double longitude; 
    double latitude; 
@end 

@implementation LTAnnotation 

@synthesize title; 
@synthesize subTitle; 
-(id) initWithTitle:(NSString*)pTitle latitude:(NSNumber*)latDbl longitude:(NSNumber*) longDbl{ 
    self = [super init]; 

    self.title = pTitle; 

    latitude = [latDbl doubleValue]; 
    longitude = [longDbl doubleValue]; 
    NSLog(@"Create Annotation for %@ at %fN %fE",pTitle,[latDbl doubleValue],[longDbl doubleValue]); 
    return self; 

} 


-(CLLocationCoordinate2D) coordinate 
{ 
    CLLocationCoordinate2D retVal; 

    retVal.latitude = latitude; 
    retVal.longitude = longitude; 

    return retVal; 
} 
@end 

個這一切結合產生這個...

alt text http://img340.imageshack.us/img340/3788/picture1fg.png

在我要去哪裏錯了任何想法? 謝謝

回答

1

兩個小東西,我注意到這可能有助於解決問題:

  • 你是不是釋放第一個代碼樣本中的引腳,將創建一個泄漏
  • 你是不是檢查如果「self = [super init];」在你的第二個代碼示例是成功的(「if(self = [super init]){...} return self」)。該NSLog的也只輸出傳遞給init方法的參數,而不是對象

和實例方法最重要的是,我只注意到這個在您的init方法:

latitude = [latDbl doubleValue]; 
longitude = [longDbl doubleValue]; 

您未使用Objective-C 2樣式存取器方法(self.latitude = ...)並且不保留自動釋放值。這可能意味着變量正在消失,這就是爲什麼你看不到註釋,因爲它們沒有有效的座標。

+0

在我的部分,馬虎編碼,我相信你是正確的第三點,改變價值屬性固定它。謝謝 – Jonathan 2009-11-10 20:50:51

+0

在JSON解析後查看我的anser -viewForAnnotation僅顯示MKMapView上的一個單獨註釋 – blackjacx 2015-10-06 11:16:22