MKMapView 不刷新注释

发布于 2025-01-03 06:52:34 字数 3844 浏览 3 评论 0原文

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

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

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

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

  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;

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

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. Adds the new annotations to an array, then adds to the map by [mapView addAnnotations:NSArray].

  2. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩起发的微风 2025-01-10 06:52:34

我猜测 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文