我正在加载一个地图视图。我正在根据通讯录中联系人的地址在地图上添加pin。我遇到了一些奇怪的情况,有些引脚(可能是10个引脚中的2个)没有掉到地图视图中。我已经核对了地址,它是有效的。但是大头针没有掉下来。这可能是什么原因。
for (i = 0; i<[groupContentArray count]; i++) {
person = ABAddressBookGetPersonWithRecordID(addressbook,[[groupContentArray objectAtIndex:i] intValue]);
ABMultiValueRef addressProperty = ABRecordCopyValue(person, kABPersonAddressProperty);
NSArray *address = (NSArray *)ABMultiValueCopyArrayOfAllValues(addressProperty);
NSLog(@"Address %@", address);
for (NSDictionary *addressDict in address)
{
addAnnotation = nil;
firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
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];
mapCenter = [self getLocationFromAddressString:fullAddress];
if(stateName != NULL || country != NULL || streetName != NULL || cityName != NULL){
if ((mapCenter.latitude == 0) && (mapCenter.longitude == 0))
{
// Alert View
}
else{
addAnnotation = (MyAddressAnnotation *)[mapView dequeueReusableAnnotationViewWithIdentifier:[groupContentArray objectAtIndex:i]];
if(addAnnotation == nil){
addAnnotation = [[[MyAddressAnnotation alloc] initWithCoordinate:mapCenter title:firstName SubTitle:lastName Recordid:[groupContentArray objectAtIndex:i] ]autorelease];
[mapView addAnnotation:addAnnotation];}
}
}
}
CFRelease(addressProperty);
}
编辑:我正在编辑我的问题,并更加具体。我正在使用该代码在我的mapView上放置pin。在我的Mac电脑上,如果有20个引脚要放在地图上。所有20个引脚都下落准确,工作良好。但当我使用IPhone或IPad时,引脚数量会大幅减少。也就是说,在我掉落的大约20个引脚中,只有4个引脚显示在地图上。我想不出这样做的原因。
此代码:
else {
addAnnotation = (MyAddressAnnotation *)[mapView dequeueReusableAnnotationViewWithIdentifier:[groupContentArray objectAtIndex:i]];
if (addAnnotation == nil) {
addAnnotation = [[[MyAddressAnnotation alloc] initWithCoordinate:mapCenter title:firstName SubTitle:lastName Recordid:[groupContentArray objectAtIndex:i] ]autorelease];
[mapView addAnnotation:addAnnotation];
}
}
毫无意义。
dequeueReusableAnnotationViewWithIdentifier
方法用于对MKAnnotationView
对象进行排队,而不是对id<MKAnnotation>
对象进行排队。
出队代码无论如何都属于viewForAnnotation
委托方法,而不是这里。
在这里,您只需要创建注释对象(MyAddressAnnotation
)并对其调用addAnnotation
。
我还强烈建议您将注释变量的名称从addAnnotation
更改为其他名称,这样就不会将其与地图视图的方法addAnnotation
混淆。
我的坏。在我使用的"fulladdress"字符串(介于streetName和cityName之间)中,没有用空格或","或"/"分隔。因此,一些地址没有被识别。