0

在我的应用程序中,我必须显示集群 Pins。这是我遵循的步骤。我的视图控制器是SummaryViewController

1) 将 MKMapview 拖放到我的 ViewController 中。

2)将该 MapView 的类设置为 OCMapView 并将委托设置为 FileOwner

3) 为该 MapView 创建了 Outlet。

@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;
        }

我的问题是引脚没有聚集。请给我最好的方法来做到这一点。

4

0 回答 0