ios - MapView随机标注图像

标签 ios mkmapview mapkit mkannotation mkannotationview

我很难弄清楚我在这里做错了什么...... 我通过循环向我的 map View 添加注释,但注释图像每次都是完全随机的...当我 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];
    }
}

提前致谢:)

最佳答案

您有两种类型的注释,但您仅在最初创建注释时设置图像。因此,如果酒吧的注释滚动离开 map View ,而俱乐部的另一个注释滚动,则可能会重用酒吧的俱乐部注释 View 。

有两种方法可以解决此问题:

  1. 对两种类型的注释 View 使用不同的reuseIdentifier参数;或

  2. 无论您调用 dequeueReusableAnnotationViewWithIdentifier 是否成功返回值,都设置/重置注释 View 的图像


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

  1. 在检查 @"Bar"@"Club 时,您可能需要使用 isEqualToString 而不是 isEqual

  2. 如果这些 if 子句都不返回 true,请确保返回 nil(这种情况永远不会发生,但尽管如此,您在此例程中仍有潜在路径)您忽略返回任何值)。我猜想,如果您通过静态分析器(Xcode 的“产品”菜单上的“分析”)运行代码,您会注意到这一点。

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

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

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

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

关于ios - MapView随机标注图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18946010/

相关文章:

ios - 为什么第二次在 iOS 上执行事件会更快?

ios - 如何关闭模态视图 Controller ,然后立即让呈现 View Controller 呈现不同的模态视图 Controller ?

ios - CLLocation- 用户位置在 App 恢复时丢失(零纬度和经度)

ios - 使用上次结果而不是新值运行的计算器

objective-c - self.tableView reloadData 不重新加载 UITableView

ios - 保存 map 注释

ios - 在跟踪用户位置时保留 map 缩放级别

ios - 使用经度和纬度获取位置 iOS 6 的方向

ios - Annotation.coordinate 不匹配 coordinate.latitude 和 coordinate.longitude

objective-c - MKMapView 设置区域 : crashes at edge of world