2012-04-17 67 views
0

我正在用引腳加載mapview。根據地址簿中人員的地址,引腳落在該位置。如果該人有一個地址,則地圖加載正常。如果他有多個地址,則只有一個地址位置顯示在地圖上。但代碼會執行以加載地址簿中個人的所有地址位置。這是我所做的。加載到地圖中,地址簿中單個人的多個地址

for (NSDictionary *addressDict in address) 
    { 
     firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); 
     lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); 
     // contactName = [firstName stringByAppendingString:lastName]; 
     NSString *country = [addressDict objectForKey:@"Country"]; 
     NSString *streetName = [addressDict objectForKey:@"Street"]; 
     NSString *cityName = [addressDict objectForKey:@"City"]; 
     NSString *stateName = [addressDict objectForKey:@"State"]; 

     if (streetName == (id)[NSNull null] || streetName.length == 0) streetName = @""; 
     if (stateName == (id)[NSNull null] || stateName.length == 0) stateName = @""; 
     if (cityName == (id)[NSNull null] || cityName.length == 0) cityName = @""; 
     if (country == (id)[NSNull null] || country.length == 0) country = @""; 

     NSString *fullAddress = [streetName stringByAppendingFormat:@"/%@/%@/%@", cityName, stateName, country]; 
     NSLog(@"full address %@", fullAddress); 

     mapCenter = [self getLocationFromAddressString:fullAddress]; 
     if(stateName != NULL || country != NULL || streetName != NULL || cityName != NULL){ 

      if ((mapCenter.latitude == 0) && (mapCenter.longitude == 0)) 
      { 
       // Alert view      
      } 
      else{ 

      addressFieldSearchBar.text = fullAddress; 
      [NSThread detachNewThreadSelector:@selector(displayMYMap) toTarget:self withObject:nil]; 
     } 
     } 
    } 


-(void)displayMYMap 
{ 
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

MKCoordinateRegion region = mapView.region; 
MKCoordinateSpan span; 
span.latitudeDelta=100; 
span.longitudeDelta=100; 


if(addAnnotation == nil){ 
    if([firstName length] < 1 && [lastName length] < 1){ 
     firstName = @"No Name"; 
     lastName = @""; 
    } 
    else if([firstName length] < 1){ 

     firstName = lastName; 
    } 

    addAnnotation = [[[MyAddressAnnotation alloc] initWithCoordinate:mapCenter title:firstName SubTitle:lastName Recordid:passedRecordID]autorelease]; 
    [mapView addAnnotation:addAnnotation]; 
} 
else{ 
    //  Alert view 
} 

編輯:當我NSLog地址,我得到地址簿中的所有地址。當我調試時,加載mapview的代碼將在所有地址情況下執行。但在地圖中,只有一個別針(人的首地址)被加載。有什麼建議麼?

+0

您可以在記錄日誌時添加獲取的日誌(或其示例)? – Manuel 2012-04-17 13:04:52

+0

2012-04-17 16:51:40.740 myProj [3926:15803]完整地址//紐約州/美國 2012-04-17 16:51:48.981 myProj [3926:15803]完整地址//芝加哥/美國狀態 – 2012-04-17 13:11:38

+0

您不應該在非主線程上與MKMapView進行交互作爲開始! – 2012-04-17 13:38:15

回答

1

由於-(void)displayMYMap中的條件if(addAnnotation == nil),您的第二個地址未加載。當你的第一個註釋完成後,addAnnotation不再是零。更改條件並檢查