下面是我的代码,用于将自定义注释加载到数组中,然后使用 addAnnotations 将此数组添加到我的 map 中。我的 map 上没有任何图钉。如果我将 [annotations addObject:annotation];
替换为 [annotations addObject:item.placemark];
我会得到图钉,但它们不是我的自定义注释。我有一个名为 Annotation 的自定义Annotation 类。我做错了什么?
NSMutableArray *annotations = [NSMutableArray array];
[response.mapItems enumerateObjectsUsingBlock:^(MKMapItem *item, NSUInteger idx, BOOL *stop) {
// if we already have an annotation for this MKMapItem,
// just return because you don't have to add it again
for (id<MKAnnotation>annotation in mapView.annotations)
{
if (annotation.coordinate.latitude == item.placemark.coordinate.latitude &&
annotation.coordinate.longitude == item.placemark.coordinate.longitude)
{
return;
}
}
// otherwise add it
Annotation *annotation = [[Annotation alloc] initWithPlacemark:item.placemark];
annotation.title = mapItem.name;
annotation.subtitle = mapItem.placemark.addressDictionary[(NSString *)kABPersonAddressStreetKey];
[annotations addObject:annotation];
[self.mapView addAnnotations:annotations];
最佳答案
问题很简单,只能是(a)添加注释的问题;或 (b) 显示注释的问题。我最初怀疑是前者(考虑到你奇怪的缩进,我怀疑你可能对一些你没有与我们分享的异步调用有一些问题),但考虑到你说如果你使用地标而不是注释,它就可以正常工作,我怀疑问题一定出在注释的显示上(即 viewForAnnotation
)。
我首先建议您做一些诊断工作。您可以通过在 addAnnotations
调用之前和之后记录 self.mapView.annotations
的 count
来确认注释是否正确添加。我怀疑(根据你所说的)你会看到计数上升。
然后我会查看 viewForAnnotation
,并确保它正确处理您的 Annotation
类。我们经常有 if ([annotation isKindOfClass:...)
逻辑,所以也许你在那里遇到一些问题。根据与我们共享的一小段代码很难说。
关于ios - 自定义注释未加载到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16565898/