ios - 自定义注释未加载到数组中?

标签 ios annotations

下面是我的代码,用于将自定义注释加载到数组中,然后使用 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.annotationscount 来确认注释是否正确添加。我怀疑(根据你所说的)你会看到计数上升。

然后我会查看 viewForAnnotation,并确保它正确处理您的 Annotation 类。我们经常有 if ([annotation isKindOfClass:...) 逻辑,所以也许你在那里遇到一些问题。根据与我们共享的一小段代码很难说。

关于ios - 自定义注释未加载到数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16565898/

相关文章:

java - TestNG Listener 的 TestNG BeforeClass 和 AfterMethod 等效方法

iphone - 如何将armv6架构添加到Xcode项目中?

ios - Swift UITableView 多行单元格

ios - Ionic 插件开发最佳实践

java - JPA 2.0 : what is javax. 验证。* 包?

c# - JetBrains.Annotations 是否应该在我的 csproj 文件中声明为私有(private) Assets ?

iphone - map 加载后显示带标题(注释)的 Pin 图

ios - Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误域=NSCocoaErrorDomain 代码=3840

ios - 如何在 iOS 中使用 DatePicker 只接受月份和年份

java - 使用注释的 Guice 方法注入(inject)