MKMapView does not update annotations

I have MKMapView (obviously) that shows the location of the housing around the user.

I have a Radius tool which, when made a choice, annotations should add / remove based on the distance around the user.

I have the addition / removal of a fine, but for some reason, the annotations will not appear until I zoom in or out.

This is a method that adds / removes annotations based on distance. I tried two different variations of the method.

  • Adds new annotations to the array, then adds to the map [mapView addAnnotations:NSArray].

  • Add annotations when they find them using [mapView addAnnotation:MKMapAnnotation];


1.

- (void)updateBasedDistance:(NSNumber *)distance {

    //Setup increment for HUD animation loading
    float hudIncrement = ( 1.0f / [[[[self appDelegate] rssParser]rssItems] count]);

    //Remove all the current annotations from the map
    [self._mapView removeAnnotations:self._mapView.annotations];

    //Hold all the new annotations to add to map
    NSMutableArray *tempAnnotations;

    /* 
     I have an array that holds all the annotations on the map becuase 
     a lot of filtering/searching happens. So for memory reasons it is
     more efficient to load annoations once then add/remove as needed.
    */
    for (int i = 0; i < [annotations count]; i++) {

        //Current annotations location
        CLLocation *tempLoc = [[CLLocation alloc] initWithLatitude:[[annotations objectAtIndex:i] coordinate].latitude longitude:[[annotations objectAtIndex:i] coordinate].longitude];

        //Distance of current annotaiton from user location converted to miles
        CLLocationDistance miles = [self._mapView.userLocation.location distanceFromLocation:tempLoc] * 0.000621371192;

        //If distance is less than user selection, add it to the map. 
        if (miles <= [distance floatValue]){
            if (tempAnnotations == nil)
                tempAnnotations = [[NSMutableArray alloc] init];
            [tempAnnotations addObject:[annotations objectAtIndex:i]];
        }

        //For some reason, even with ARC, helps a little with memory consumption
        tempLoc = nil;

        //Update a progress HUD I use. 
        HUD.progress += hudIncrement;
    }

    //Add the new annotaitons to the map
    if (tempAnnotations != nil)
        [self._mapView addAnnotations:tempAnnotations];
}

2.

- (void)updateBasedDistance:(NSNumber *)distance {

    //Setup increment for HUD animation loading
    float hudIncrement = ( 1.0f / [[[[self appDelegate] rssParser]rssItems] count]);

    //Remove all the current annotations from the map
    [self._mapView removeAnnotations:self._mapView.annotations];

    /* 
     I have an array that holds all the annotations on the map becuase 
     a lot of filtering/searching happens. So for memory reasons it is
     more efficient to load annoations once then add/remove as needed.
    */
    for (int i = 0; i < [annotations count]; i++) {

        //Current annotations location
        CLLocation *tempLoc = [[CLLocation alloc] initWithLatitude:[[annotations objectAtIndex:i] coordinate].latitude longitude:[[annotations objectAtIndex:i] coordinate].longitude];

        //Distance of current annotaiton from user location converted to miles
        CLLocationDistance miles = [self._mapView.userLocation.location distanceFromLocation:tempLoc] * 0.000621371192;

        //If distance is less than user selection, add it to the map. 
        if (miles <= [distance floatValue])
            [self._mapView addAnnotation:[annotations objectAtIndex:i]];

        //For some reason, even with ARC, helps a little with memory consumption
        tempLoc = nil;

        //Update a progress HUD I use. 
        HUD.progress += hudIncrement;
    }
}

I also tried at the end of the method described above:

[self._mapView setNeedsDisplay];
[self._mapView setNeedsLayout];

Also, to force update (somewhere this might work):

self._mapView.showsUserLocation = NO;
self._mapView.showsUserLocation = YES;

, , , .

+1
1

, updateBasedDistance: . NSLog(@"Am I in the UI thread? %d", [NSThread isMainThread]);. 0, removeAnnotations: addAnnotation: performSelectorOnMainThread: GCD .

+10

Source: https://habr.com/ru/post/1545756/


All Articles