【问题标题】:Add annotations to MKMapView with different images according to location of object根据对象的位置为MKMapView添加不同图像的注释
【发布时间】:2016-03-09 11:35:23
【问题描述】:

我一直在寻找几个小时来找到一个关于如何在给定包含对象位置和图像的类(在我的例子中,对象称为 EVENT)的情况下添加注释的好教程。

看看我到目前为止做了什么,我从 viewDidLoad 调用这个方法 setupAnnotations

- (void)setupAnnotations {

    _allEvents = [EventEntity MR_findAll];

    for (EventEntity *event in _allEvents) {

        AVEventAnnotationView *annotation = [[AVEventAnnotationView alloc] initWithOperator:event];

        CLLocation *eventLocation = [[CLLocation alloc] initWithLatitude:[[event latitude] doubleValue] longitude:[[event longitude] doubleValue]];
        annotation.coordinate = eventLocation.coordinate;

        MKPlacemark *eventPlacemark = [[MKPlacemark alloc] initWithCoordinate:eventLocation.coordinate addressDictionary:nil];
        MKMapItem *eventItem = [[MKMapItem alloc] initWithPlacemark:eventPlacemark];

        // Im loading image from URL for or based on event object
        UIImageView *imageContainer = [[UIImageView alloc] init];
        [imageContainer sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",fullBannerImgPath,[event bannerImg]]]];
        [_bannerImagesArray addObject:imageContainer];

        BOOL hasAnnotation = NO;
        for (AVEventAnnotationView *ann in self.mapView.annotations) {
            if([ann isKindOfClass:[AVEventAnnotationView class]])
            {
                if([[annotation.eventEntity dataId] isEqualToString:[ann.eventEntity dataId]])
                {
                    hasAnnotation = YES;
                    break;
                }
            }
        }

        if(!hasAnnotation)
            [self.mapView addAnnotation:annotation];
    }
}

这是我的 mapview 委托:

- (MKAnnotationView *)mapView:(MKMapView *)mapViewIn viewForAnnotation:(id <MKAnnotation>)annotation {
    if (mapViewIn != self.mapView || [annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }

    static NSString *annotationIdentifier = @"SPGooglePlacesAutocompleteAnnotation";
    MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];

    // STUCK HERE
    annotationView.image = [UIImage imageNamed:@"LocationBlue"];
    annotationView.frame = CGRectMake(0, 0, 80, 80);
    annotationView.draggable = NO;
    annotationView.canShowCallout = YES;

    return annotationView;
}

我是实施 MKMapView 的新手。前段时间刚看了注解和注解视图的区别。

无论如何,回到我的主要问题或我的主要问题:如果我有一个包含纬度、经度和图像的事件数组,如何将注释/注释视图放入 mkmapview?

【问题讨论】:

    标签: ios mkmapview mkannotationview


    【解决方案1】:

    我不知道什么是 AVAnnotationView 但我会说你不需要它。您只需使您的 Event 类符合 MKAnnotation 协议,即将 CLLocationCoordinate2D 类型的属性坐标添加到 Event 类。在此之后,只需使用 addAnnotations 将事件添加到地图。

    在您的 mapView:viewForAnnotation: 中,您确定正在显示的注释是什么(正在显示的事件是什么),因此您获得了要显示的图像(您可以使用标签或任何其他方式)和设置注释视图的图像。

    或者你只是说:

    if event = annotation as? AVEventAnnotationView { 
    //add your image and do stuff
    }
    

    【讨论】:

    • 等等,我现在明白了。
    猜你喜欢
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多