mapview随机注释图像

时间:2013-09-22 16:22:46

标签: ios mkmapview mapkit mkannotation mkannotationview

我很难搞清楚我在这里做错了什么...... 我正在通过循环向我的mapview添加注释,但注释图像每次都是完全随机的...当我NSLog订单时,它是随机的 - 我不确定这是否是问题

- (MKAnnotationView *)mapView:(MKMapView *)mapView1 viewForAnnotation:(id<MKAnnotation>)annotation {

    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    if ([[annotation subtitle] isEqual:@"Bar"]) {

        MKAnnotationView *view = nil;

        view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"];
        if (!view) {
            // Could not reuse a view ...

            // Creating a new annotation view
            view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"];


            view.enabled = YES;
            view.canShowCallout = YES;
            view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            view.image = [UIImage imageNamed:@"beer.png"];
        }

        return view;
    }
    else if ([[annotation subtitle] isEqual:@"Club"]) {

        MKAnnotationView *view = nil;

        view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"];
        if (!view) {
            // Could not reuse a view ...

            // Creating a new annotation view
            view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"];


            view.enabled = YES;
            view.canShowCallout = YES;
            view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            view.image = [UIImage imageNamed:@"clubs.png"];
        }

        return view;
    }
}

view.image是完全随机的...... clubs.pngbeer.png .. 我该如何正确使用?

这是我添加注释的方式:

- (void)barLoop {

    for (int i = 0; i<barArray.count; i++) {

        int index = [[barArray objectAtIndex:i]intValue];

        NSString *lati = [[[self.usersLocationArray objectAtIndex:index]
                           valueForKeyPath:@"items.Latitude"]componentsJoinedByString:@""];
        NSString *longi = [[[self.usersLocationArray objectAtIndex:index]
                            valueForKeyPath:@"items.Longitude"]componentsJoinedByString:@""];
        NSString *barNavn = [[[self.usersLocationArray objectAtIndex:index] valueForKeyPath:@"items.Navn"]componentsJoinedByString:@""];


        float latitude = [lati floatValue];
        float longitude = [longi floatValue];

        MKCoordinateRegion region = { {0.0, 0.0} , {0.0, 0.0} };
        region.center.latitude = latitude;
        region.center.longitude = longitude;
        region.span.longitudeDelta = 0.20f;
        region.span.latitudeDelta = 0.20f;
        [mapView setRegion:region animated:NO];

        CLLocationCoordinate2D location;
        location.latitude = latitude;
        location.longitude = longitude;
        Annotation *ann = [[Annotation alloc]initWithPosition:location];
        ann.title = barNavn;
        ann.subtitle = @"Bar";
        [self.mapView addAnnotation:ann];
    }
}

提前感谢:)

1 个答案:

答案 0 :(得分:3)

您有两种类型的注释,但您只是在最初创建注释时设置图像。因此,如果条形图的注释滚动地图视图并且滚动条的另一个注释滚动,则它可能会重复使用条形图的注释视图。

有两种解决方法:

  1. 为两种类型的注释视图中的每一种使用不同的reuseIdentifier参数;或

  2. 设置/重置注释视图image,无论您对dequeueReusableAnnotationViewWithIdentifier的调用是否成功返回值。


  3. 不相关,但您的viewForAnnotation方法应该:

    1. 在检查isEqualToString vs isEqual时,您可能希望使用@"Bar"代替@"Club"

    2. 如果这些nil条款都没有返回true,请确保返回if(这应该永远不会发生,但是在此例程中你有一条潜在的路径,你忽略了值)。如果您通过静态分析器运行代码(Xcode的“产品”菜单上的“分析”),我猜这会引起您的注意。

    3. 一个更微妙的观察,但我可能不会依赖注释的subtitle来确定要使用哪个注释视图,而是

      • 有两个注释子类,一个用于条形,一个用于俱乐部;或

      • 在您现有的Annotation课程中有一个自定义属性,表明它是一个酒吧还是俱乐部(我可能会使用enum)。

      在将来某个日期,您可能希望将标注的副标题用于“Bar”与“Club”之外的其他内容(例如,可能是酒吧/俱乐部的地址?),并且当前模型似乎会混淆UI带有状态属性的属性(即在callout中显示的属性)(即控制要使用的注释视图类型的变量)。没什么大不了的,只是一个建议。

相关问题