iOS: MKAnnotaion: MapView не кластеризован

В моем приложении я должен отобразить кластерные контакты. Вот шаги, которым я следовал, чтобы сделать это. Мой ViewController это SummaryViewController

1) Поместите и поместите MKMapview в мой ViewController.

2) Установите класс для этого MapView как OCMapView и установите делегата в FileOwner

3) Создан выход для этого MapView.

@interface SummaryViewController : 
    UIViewController< MKMapViewDelegate, PopViewControllerDelegate, UIPopoverControllerDelegate>
       @property (weak, nonatomic) IBOutlet OCMapView *mapView;
@end

4) В ViewDidLoad

mapView.delegate = self;
    mapView.clusterSize = 0.2;
    mapView.clusterByGroupTag = YES;

5) Добавлены аннотации

(void) displayPushpins: (NSArray *)responseString{

        for (id<MKAnnotation> annotation in mapView.annotations) {
            [mapView removeOverlays:mapView.overlays];
            [mapView removeAnnotation:annotation];
        }
    for (int i=0; i< [responseString count];i++){
        NSDictionary *data = [responseString objectAtIndex:i];
        NSString * details= [NSString stringWithFormat:@"%@",[data objectForKey:@"Inc_key"]];
        NSString * crimeDescription =[data objectForKey:@"Inc_Code_descr"];
        NSString * inc_address = [data objectForKey:@"Address"];
        CLLocationCoordinate2D coordinate;
        coordinate.latitude = [[data objectForKey:@"Latitude"]doubleValue];
        coordinate.longitude = [[data objectForKey:@"Longitude"]doubleValue];
        MyLocation *annotation = [[MyLocation alloc] initWithName:crimeDescription address:inc_address description:details coordinate:coordinate];

        [mapView addAnnotation:annotation];
    }
    NSLog(@"Total Pins:%d",[responseString count]);

}

6) Методы делегирования

- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if([aMapView isKindOfClass:[OCMapView class]]){

        static NSString *identifier = @"MyLocation";
        MKAnnotationView *annotationView = (MKAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        // if it's a cluster
        if ([annotation isKindOfClass:[OCAnnotation class]]) {
            NSLog(@"annotation:OCAnnotation");
            OCAnnotation *clusterAnnotation = (OCAnnotation *)annotation;

            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ClusterView"];
            annotationView.canShowCallout = YES;
            annotationView.centerOffset = CGPointMake(0, -20);


            // set title
            clusterAnnotation.title = @"Cluster";

            clusterAnnotation.subtitle = [NSString stringWithFormat:@"Containing incidents: %d", [clusterAnnotation.annotationsInCluster count]];

            UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 20, 20)] ;

            lbl.backgroundColor = [UIColor clearColor];
            lbl.textColor = [UIColor whiteColor];
            [lbl setTextAlignment:UITextAlignmentCenter];
            [lbl setFont:[UIFont fontWithName:@"Arial" size:12]];
            lbl.text = nil;
            lbl.text = [NSString stringWithFormat:@"%d", [clusterAnnotation.annotationsInCluster count]];
            [annotationView addSubview:lbl];
            // set its image
            annotationView.image = [UIImage imageNamed:@"cluster_pin.png"];

            [annotationView setUserInteractionEnabled: YES];
            UIButton *discloseButton = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];

            annotationView.leftCalloutAccessoryView = discloseButton;

            if (mapView.clusterByGroupTag) {
                    annotationView.image = [UIImage imageNamed:@"cluster_pin.png"];


                clusterAnnotation.title = clusterAnnotation.groupTag;
            }

            return annotationView;
        }


        if ([annotation isKindOfClass:[MyLocation class]]) {
            NSLog(@"annotation:MyLocation");
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            annotationView.enabled = YES;
            annotationView.canShowCallout = YES;
            annotationView.image=[UIImage imageNamed:@"pushpin_normal.png"];

            [annotationView setUserInteractionEnabled: YES];
            UIButton *discloseButton = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
            //[discloseButton addTarget: self action: @selector(showMyView:) forControlEvents: UIControlEventTouchUpInside];

            annotationView.leftCalloutAccessoryView = discloseButton;
            //here we use a nice image instead of the default pins
            return annotationView;

        }
    }
    else{
        NSLog(@"miniMap");

    }

    return nil;
}

7) Добавить полигон

  - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
    {
        if ([overlay isKindOfClass:[MKPolygon class]])
        {
            MKPolygonView* aView = [[MKPolygonView alloc] initWithPolygon:(MKPolygon*)overlay];

            aView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
            aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
            aView.lineWidth = 3;

            return aView;
        }

Моя проблема в том, что контакты не сгруппированы. Пожалуйста, предоставьте мне лучший способ сделать это.

0 ответов

Другие вопросы по тегам