MKMapView没有刷新注释

时间:2012-02-07 03:01:15

标签: objective-c ios xcode annotations mkmapview

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

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

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

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

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

  2. 使用[mapView addAnnotation:MKMapAnnotation];

  3. 添加注释。

    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;
    

    非常感谢任何帮助,并且一如既往,感谢您抽出宝贵时间阅读。

1 个答案:

答案 0 :(得分:10)

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