本文介绍了MKMapView 不刷新注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a MKMapView (obviously), that shows housing locations around the user.

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

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

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

I have it add/removing fine but for some reason the annotations won't show up until I zoom in or out.

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

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

  1. 将新的注解添加到数组中,然后通过[mapView addAnnotations:NSArray]添加到地图中.

使用 [mapView addAnnotation:MKMapAnnotation] 在找到注释时添加注释;

Add the annotations as it finds 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 have also attempted at the end of the above method:

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

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

Also, to force a refresh (saw somewhere it might work):

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

非常感谢任何帮助,并一如既往地感谢您花时间阅读.

Any help would be very much appreciated and as always, thank you for taking the time to read.

推荐答案

我猜测 updateBasedDistance: 是从后台线程调用的.检查 NSLog(@"我在 UI 线程中吗?%d", [NSThread isMainThread]);.如果它是 0,那么您应该将 removeAnnotations:addAnnotation: 移动到 performSelectorOnMainThread: 调用,或者使用GCD 在主线程上阻塞.

I'm going to guess that updateBasedDistance: gets called from a background thread. Check with NSLog(@"Am I in the UI thread? %d", [NSThread isMainThread]);. If it's 0, then you should move the removeAnnotations: and addAnnotation: to a performSelectorOnMainThread: invocation, or with GCD blocks on the main thread.

这篇关于MKMapView 不刷新注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:37