加载到地图中,地址簿中单个人的多个地址

问题描述:

我正在用引脚加载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

由于-(void)displayMYMap中的条件if(addAnnotation == nil),您的第二个地址未加载。当你的第一个注释完成后,addAnnotation不再是零。更改条件并检查