dimanche 26 avril 2015

Get total number of annotations of visible cluster/s in mkmapview


Can anyone help me with MKMapViews. I am using the FBAnnotations/FBClustering.

My issue is, for example, lets say I have 20 annotations and is clustered in to 4. I have 2 clusters visible in the map, cluster A with 3 annotations and cluster B with 4 annotations.

I want the total of cluster A and B be the title of the view controller (in my case it should be 7).
Instead of getting 7 I keep getting the total number of annotations in my map.

I uploaded an image, please click here.
Here's what i've got so far:
This is the method in my MKMapView view controller:

- (void)mapViewWithJobsDisplayed:(NSDictionary *)jobsDictionary andUserLocation:(CLLocation *)location andUserCoordinates:(CLLocationCoordinate2D)locationCoordinates
{   
    if(locationCoordinates.latitude == 0.0f || locationCoordinates.longitude==0.0f)
    { 
    }
    else
    {
        ar = [jobsDictionary objectForKey:@"sub_slots"];

        //Save location
        userLocation = location;
        userLocationCoordinates = locationCoordinates;

        // Initiate cluster array
        clusterArray = [[NSMutableArray alloc] initWithCapacity:ar.count];

        for (int i = 0; i < ar.count; i++) {
            NSDictionary *jobPin = [ar objectAtIndex:i];
            latitude = [[jobPin objectForKey:@"sub_slots"] objectForKey:@"latitude"];
            longitude = [[jobPin objectForKey:@"sub_slots"] objectForKey:@"longitude"];
            companyName = [[jobPin objectForKey:@"sub_slots"] objectForKey:@"company_name"];
            companyAddress = [[jobPin objectForKey:@"sub_slots"] objectForKey:@"address"];

            CLLocationCoordinate2D coordinate;
            coordinate.latitude = [latitude doubleValue];
            coordinate.longitude = [longitude doubleValue];

            if([latitude doubleValue] == 0.0f || [longitude doubleValue]==0.0f)
            {
            }
            else
            {
                MyLocation *annotation = [[MyLocation alloc] initWithName:companyName address:companyAddress coordinate:coordinate];
                [annotation setTagNumber:i];
                [self addAnnotation:annotation];
                [clusterArray addObject:annotation]; 
            }
        }

        //Create clustering manager
        clusteringManager = [[FBClusteringManager alloc] initWithAnnotations:clusterArray];
        clusteringManager.delegate = self;
        [self refreshMapView];
    }
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKAnnotationView *av = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"JobsPins"];

    UIImage *backgroundImage = [UIImage imageNamed:@"pinMap"];
    if ([annotation isKindOfClass:[FBAnnotationCluster class]])
    {
        FBAnnotationCluster *cluster = (FBAnnotationCluster *)annotation;
        cluster.title = [NSString stringWithFormat:@"%lu", (unsigned long)cluster.annotations.count];
        av.canShowCallout = YES;
        UIImage *frontImage = [self drawTextWithStroke:cluster.title andImage:backgroundImage];
        av.image = [self placeImage:frontImage onImage:backgroundImage];
        // Check zoom level and show accessory button
        NSUInteger zoomLevel = [self zoomLevelForMapRect:mapView.visibleMapRect withMapViewSizeInPixels:mapView.bounds.size];

        if (zoomLevel > 1) {
            av.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        }

        return av;
    }

    if ([annotation isKindOfClass:[MyLocation class]])
    {
        av.canShowCallout = YES;
        UIImage *frontImage = [self drawTextWithStroke:@"1" andImage:backgroundImage];
        av.image = [self placeImage:frontImage onImage:backgroundImage];
        av.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        return av;
    }

    return nil;
}


Aucun commentaire:

Enregistrer un commentaire