MKMapView未刷新注释



我有一个MKMapView(显然),它显示了用户周围的住房位置。

我有一个Radius工具,当进行选择时,应该根据用户周围的距离添加/删除注释。

我可以很好地添加/删除它,但由于某种原因,在我放大或缩小之前,注释不会显示。

这是一种根据距离添加/删除注释的方法。我试过两种不同的方法。

  1. 将新注释添加到数组中,然后按[mapView addAnnotations:NSArray]添加到映射中。

  2. 在使用[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;
    }
}

我也尝试过在上述方法的末尾:

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

此外,为了强制刷新(看到它可能工作的地方):

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

如有任何帮助,我们将不胜感激,并一如既往地感谢您抽出时间阅读。

我猜updateBasedDistance:是从后台线程调用的。用NSLog(@"Am I in the UI thread? %d", [NSThread isMainThread]);检查。如果是0,那么应该将removeAnnotations:addAnnotation:移动到performSelectorOnMainThread:调用,或者在主线程上使用GCD块。

最新更新