0
votes

In my application, I was published recorded video from particular location. I can get the list of published location details from another service response. so Initially I got that published location details and displayed it on MapView. In MapView the pins are displayed with Cluster effect, so I used kingpin.

Here below I have loaded the Annotations on Map.

- (void)loadLocations:(NSArray *)arrayValues 
{
    _annotationArray = arrayValues;

    [self.clusteringController setAnnotations:[self reSetannotations]];

    [self.mapView setZoomEnabled:YES];

    [self.mapView setCenterCoordinate:self.mapView.userLocation.coordinate];

    [self.mapView setUserTrackingMode:MKUserTrackingModeFollow];


    KPGridClusteringAlgorithm *algorithm = [KPGridClusteringAlgorithm new];

    algorithm.annotationSize = CGSizeMake(25, 50);

    algorithm.clusteringStrategy = KPGridClusteringAlgorithmStrategyTwoPhase;

    self.clusteringController = [[KPClusteringController alloc] initWithMapView:self.mapView
                                                            clusteringAlgorithm:algorithm];
    self.clusteringController.delegate = self;

    self.clusteringController.animationOptions = UIViewAnimationOptionCurveEaseOut;

    [self.clusteringController setAnnotations:[self annotations]];

    NSString * lastobjlat;

    double miles;

    CLLocation * location = [COMMON currentLocation];

    lastobjlat = [NSString stringWithFormat:@"%f",location.coordinate.latitude];

    miles = 1.;

    double scalingFactor = ABS( (cos(2 * M_PI * [lastobjlat floatValue] / 360.0) ));

    MKCoordinateSpan span;

    span.latitudeDelta = miles/69.0;

    span.longitudeDelta = miles/(scalingFactor * 69.0);

    MKCoordinateRegion region;

    region.span = span;

    region.center = location.coordinate;

    [self.mapView setRegion:region animated:YES];

    self.mapView.showsUserLocation = YES;


    //Call in the below selectAnnotationAction method when I came to this, after I published new one on existed or new locations
    if (COMMON.isRecentPublication == YES) {

        COMMON.isRecentPublication = NO;

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            [self selectAnnotationAction];
        });
    }
}

//To reset the Annotations

- (NSArray *)reSetannotations 
{
    NSMutableArray *annotations = [NSMutableArray array];

    return annotations;
}

//Here I managed location details on my custom marker class MapAnnotation.

- (NSArray *)annotations {

    CLLocationCoordinate2D locationPort;

    NSMutableArray *annotations = [NSMutableArray array];

    NSString *latitude, *longitude;

    if ([_annotationArray count] > 0) {

        for (int i = 0; i <[_annotationArray count]; i++){

            latitude = [NSString stringWithFormat:@"%@",[[_annotationArray objectAtIndex:i] valueForKey:@"latitude"]];

            latitude = [latitude stringByReplacingOccurrencesOfString:@"\n" withString:@""];

            latitude = [latitude stringByReplacingOccurrencesOfString:@"\t" withString:@""];

            longitude = [NSString stringWithFormat:@"%@",[[_annotationArray objectAtIndex:i] valueForKey:@"longitude"]];

            longitude = [longitude stringByReplacingOccurrencesOfString:@"\n" withString:@""];

            longitude = [longitude stringByReplacingOccurrencesOfString:@"\t" withString:@""];

            latitude = [NSString replaceEmptyStringInsteadOfNull:latitude];

            longitude = [NSString replaceEmptyStringInsteadOfNull:longitude];

            int publicationRatio   = [[[_annotationArray objectAtIndex:i] valueForKey:@"publicationRatio"] intValue];

            int publicationCount   = [[[_annotationArray objectAtIndex:i] valueForKey:@"publicationsCount"] intValue];

            int teazLocationId     = [[[_annotationArray objectAtIndex:i] valueForKey:@"objectId"] intValue];

            BOOL isUpgrade         = [[[_annotationArray objectAtIndex:i] valueForKey:@"isUpgraded"] boolValue];

            locationPort = CLLocationCoordinate2DMake([latitude doubleValue] ,
                                                      [longitude doubleValue]);

            //TODO : This is my custom annotation method

            MapAnnotation *a1 = [[MapAnnotation alloc] initWithCoordinate:locationPort
                                                                            tag:i
                                                               publicationRatio:publicationRatio
                                                               publicationCount:publicationCount
                                                                 teazLocationId:teazLocationId isUpgraded:isUpgrade];

            a1.itemindex = i + 1;

            a1.publicationRatio = publicationRatio;

            a1.publicationCount = publicationCount;

            a1.teazLocationId = teazLocationId;

            a1.isUpgraded = isUpgrade;

            a1.coordinate = CLLocationCoordinate2DMake([latitude doubleValue] ,
                                                       [longitude doubleValue] );
            [annotations addObject:a1];

            if (COMMON.isRecentPublication == YES) {

                if ([COMMON.recentPublicationLocationID  isEqual: @(publishedLocationId)]) {

                    _recentAnnotationView = [[MKPinAnnotationView alloc] initWithAnnotation:a1 reuseIdentifier:@"cluster"];

                }
            }
        }
    }

    return annotations;
}

It's not a problem to load Annotations with clustering effect on initial time. But when I published some one from same or different(new also) locations, I need to redirect Map screen(publishing section is other screen) and I need to display that publication detail with Active pin image.

Following steps I made to show the published location detail on Map

  1. I created recentPublicationLocationID as a global variable and store the recentPublishedLocationID from response after published service.

  2. Then this return type method - (NSArray *)annotations(after redirect to mapView I got the location details from another webservice after that it will called),

I have compared with recentPublishedId If existed or not. Then If existed, I have assigned my custom annotation (contains the recentPublished location details) to global MKPinAnnotationView instance - _recentAnnotationView

  1. Then I directly called the didSelectPinAnnotation delegate method from this method - (void)loadLocations:(NSArray *)arrayValues like below,

//Pass recent published location details

if (COMMON.isRecentPublication == YES)
{
    COMMON.isRecentPublication = NO;

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [self selectAnnotationAction];
    });
}

//selectAnnotationAction

- (void)selectAnnotationAction {

    COMMON.isRecentPublication = NO;

    COMMON.recentPublicationLocationID = nil;

    [self mapView:self.mapView didSelectAnnotationView:_recentAnnotationView];
}

If I directly passed recentPublishedLocation details to didSelectAnnotationView delegate, I can only show the In Active pin instead of Active pin.

Then I debug with breakpoint why I can see the In active pin only ?

Because In this situation the didselect delegate was called and I can see the Active pin image. But it's only within sec.

Because viewForAnnotation delegate was called quickly for other pin annotations so the selected one goes to unselected state

This is the real problem. How can I overcome this work with clusters ?

Because when I displayed that published location detail correctly on map even it should be work with clustering effect. Yes I will zoom back to see the pins with cluster effect .

1
You really are expecting a stranger to read all these lines of text? - El Tomato
@EI Tomato I need suggestions, So I just explained what I made it. So that's why I explained huge with text. So I can't unable to ignore it. - Ram

1 Answers

0
votes

Finally I got the solution and achieved my requirement using ClusterKit library instead of Kingpin.

This tool really helpful me against possible to achieve append annotations and customize every thing adopt with my requirements.

So it's more helpful to me. Of course all of you.

And this tool supports Apple & Goole maps with Objective c as well as Swift.

I hope this one more helpful to other developers also.

Thanks :)