Arianne Lee
Arianne Lee

Reputation: 51

How to add objects in a NSMutableDictionary

I have an app that uses MKMapView. In my app, I have declared an array that will hold the response from the API. The data from the API are the jobs to be pinned in the map (clustered annotations). The array that holds the jobs from the API will/needs to be filtered by the jobs pins that are visible in the map. I am able to filter the coordinates (visible or not visible in the map) but I am having troubles on storing the data (coordinates that are visible) in a new array.

Here's what I have so far: In regionDidChangeAnimated in my mapview

        [ar objectAtIndex:0];
        NSMutableDictionary *visibleJobs;

        for(NSDictionary *loc in ar)
        {
            CLLocationDegrees Lat = [[[loc objectForKey:@"sub_slots"] objectForKey:@"latitude"] doubleValue];
            CLLocationDegrees longTitude = [[[loc objectForKey:@"sub_slots"] objectForKey:@"longitude"] doubleValue];
            CLLocationCoordinate2D point = CLLocationCoordinate2DMake(Lat, longTitude);

            MKMapPoint mkPoint = MKMapPointForCoordinate(point);

            BOOL contains = MKMapRectContainsPoint(mapView.visibleMapRect, mkPoint);
            if(contains)
            {
                NSLog(@"Contains:1");
            }
            else
            {
                NSLog(@"Contains:0");
            }
        }

Any help will be very much appreciated. Thanks!

Upvotes: 0

Views: 648

Answers (1)

Malav Soni
Malav Soni

Reputation: 2838

why don't you use NSMutableArray

try this

NSMutableArray *visibleJobs = [[NSMutableArray alloc]init];

    for(NSDictionary *loc in ar)
    {
        CLLocationDegrees Lat = [[[loc objectForKey:@"sub_slots"] objectForKey:@"latitude"] doubleValue];
        CLLocationDegrees longTitude = [[[loc objectForKey:@"sub_slots"] objectForKey:@"longitude"] doubleValue];
        CLLocationCoordinate2D point = CLLocationCoordinate2DMake(Lat, longTitude);

        MKMapPoint mkPoint = MKMapPointForCoordinate(point);

        BOOL contains = MKMapRectContainsPoint(mapView.visibleMapRect, mkPoint);
        if(contains)
        {
            NSLog(@"Contains:1");
            [visibleJobs addObject:loc];
        }
        else
        {
            NSLog(@"Contains:0");
        }
    }

Now visible jobs array contains dictionary of all pin data which in currently visible on map

Upvotes: 1

Related Questions