当将自定义注释添加到mapView时

当将自定义注释添加到mapView时

本文介绍了当将自定义注释添加到mapView时,didSelectAnnotationView会自动调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在MKMapView上添加自定义注释,并在点击注释时实现自定义callOut view.

Am trying to add custom annotations on MKMapView and implement custom callOut view on tap of annotation.

之后链接相同.问题是,当我添加自定义注释didSelectAnnotationView时会被单独调用并显示popOver callout,即使用户没有单击该注释

Am following this link for the same. The issue is when I add the custom annotation didSelectAnnotationView is called on its own and the popOver callout is shown, even when the user has not clicked the annotation

这是我的代码:

    -(void)callAddAnnotationsLocal{
    [_mapView removeAnnotations:[_mapView annotations]];

    CLLocationCoordinate2D coord = {.latitude =  43.3998170679, .longitude =  -95.1288472486};

     [_mapView addAnnotations:[self annotationsLocal]];

}

    -(NSArray *)annotationsLocal{
    self.annotArrayLocal = nil;
    self.annotArrayLocal = [[NSMutableArray alloc] init];
    for (int i=0; i<[arrAmmenities count];i++)
    {
        MKAnnotationView *propertyAnnotation = [[MKAnnotationView alloc] init];
        UIFont *font = [UIFont fontWithName:@"Arial" size:18];
        NSDictionary *userAttributes = @{NSFontAttributeName: font,
                                         NSForegroundColorAttributeName: [UIColor blackColor]};
        NSString *latStr = [NSString stringWithFormat:@"%@",[[arrAmmenities  objectAtIndex:i]objectForKey:@"latitude"]];
        float latitude = [latStr floatValue];
        NSString *longStr = [NSString stringWithFormat:@"%@",[[arrAmmenities  objectAtIndex:i]objectForKey:@"longitude"]];
        float longitude = [longStr floatValue];
        CLLocationCoordinate2D coord = {.latitude =  latitude, .longitude =  longitude};
        PinAnnotation * annotation = [[PinAnnotation alloc] init];
        [annotation setCoordinate:coord];
        [annotation setType:[[arrAmmenities objectAtIndex:i] objectForKey:@"category"]];
        [annotation setTag:i];
        [self.mapView addAnnotation:annotation];

        [self.annotArrayLocal addObject:propertyAnnotation];
    }
    return _annotArrayLocal;

}

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation {
    MKAnnotationView *annotationView;
    NSString *identifier;

        if ([annotation isKindOfClass:[PinAnnotation class]]) {
            // Pin annotation.
            identifier = @"Pin";
            annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

                annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];

                if([[(PinAnnotation *)annotation type] isEqualToString:@"Gas Stations"])
                    annotationView.image = [UIImage imageNamed:@"marker_gas"];
                else if([[(PinAnnotation *)annotation type] isEqualToString:@"Golf Courses"])
                    annotationView.image = [UIImage imageNamed:@"marker_golf"];
                else if([[(PinAnnotation *)annotation type] isEqualToString:@"Restaurants"])
                    annotationView.image = [UIImage imageNamed:@"marker_restaurant"];
                else
                    annotationView.image = [UIImage imageNamed:@"marker_hospital"];

            annotationView.canShowCallout = NO;
        }
    return annotationView;

}



 -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
//    [mapView deselectAnnotation:view.annotation animated:YES];

    MapAnnotViewController *controller = [[UIStoryboard storyboardWithName:@"MapStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"sidMapAnnotVC"];
//    controller.annotation = view.annotation; // it's useful to have property in your view controller for whatever data it needs to present the annotation's details

    wyPopController = [[WYPopoverController alloc] initWithContentViewController:controller];
    wyPopController.delegate = self;
    [wyPopController setPopoverContentSize:CGSizeMake(200.0, 100.0)];

    [wyPopController presentPopoverFromRect:view.frame
                                  inView:view.superview
                permittedArrowDirections:WYPopoverArrowDirectionAny
                                animated:YES];
}

我在哪里弄错了?我该如何解决?

Where am I getting wrong? How do I solve this?

推荐答案

一些想法:

  1. annotationsLocal中,您不仅要实例化PinAnnotation对象并将其添加到地图,而且还要实例化MKAnnotationView对象,构建它们的数组,然后返回该数组,然后将其作为注释添加到地图视图中.

  1. In annotationsLocal, you are not only instantiating PinAnnotation objects and adding them to a map, but you are also instantiating MKAnnotationView objects, building an array of them, and then returning that array, which is then added as annotations to the map view.

根本不应该构建第二个MKAnnotationView数组,当然也不应该将其添加为地图视图的注释.不要混淆注释对象(代表地图上点的坐标)和注释视图对象(代表这些注释在地图上呈现时的视觉表示).

That second array of MKAnnotationView should not be built at all, and certainly shouldn't be added as annotations of the map view. Don't conflate annotation objects (which represent the coordinates of points on a map) and annotation view objects (which represent visual representation of those annotations as they are rendered on the map).

顺便说一句,与您的问题无关,您也将在viewForAnnotation中实例化不必要的注释视图.您应该使注释视图出队,并且只有在没有成功检索要重用的视图时才实例化一个新视图:

As an aside, and unrelated to your problem, you are instantiating unnecessary annotation views in viewForAnnotation, too. You should dequeue an annotation view, and only instantiate a new one if you didn't successfully retrieve one that was to be reused:

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

    if ([annotation isKindOfClass:[PinAnnotation class]]) {
        // Pin annotation.
        NSString *identifier = @"Pin";
        annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView) {
            annotationView.annotation = annotation
        } else {
            annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            annotationView.canShowCallout = NO;
        }

        if ([[(PinAnnotation *)annotation type] isEqualToString:@"Gas Stations"])
            annotationView.image = [UIImage imageNamed:@"marker_gas"];
        else if([[(PinAnnotation *)annotation type] isEqualToString:@"Golf Courses"])
            annotationView.image = [UIImage imageNamed:@"marker_golf"];
        else if([[(PinAnnotation *)annotation type] isEqualToString:@"Restaurants"])
            annotationView.image = [UIImage imageNamed:@"marker_restaurant"];
        else
            annotationView.image = [UIImage imageNamed:@"marker_hospital"];
    }

    return annotationView;
}

但是我在这里看不到会导致调用didSelectAnnotationView的任何东西,除非将批注视图添加为批注的错误过程造成了这种奇怪的副作用.如果仍然看到didSelectAnnotationView被调用,建议您在此处添加一个断点,然后查看调用堆栈,看看是否可以看到在哪里调用了该断点.但是希望annotationsLocal的修复将解决该问题.

But I'm not seeing anything here that would cause didSelectAnnotationView to be called, unless that incorrect process of adding annotation views as annotations had some weird side effect that caused this. If you're still seeing didSelectAnnotationView called, I might suggest adding a breakpoint there and looking at the call stack and seeing if you can see where that was being invoked. But hopefully the fixing of annotationsLocal will fix it.

这篇关于当将自定义注释添加到mapView时,didSelectAnnotationView会自动调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:38