gpt4 book ai didi

objective-c - MKMapView 不刷新注释

转载 作者:行者123 更新时间:2023-12-01 17:19:51 26 4
gpt4 key购买 nike

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

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

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

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

  • 将新注释添加到数组中,然后通过 [mapView addAnnotations:NSArray] 添加到 map 中.
  • 使用 [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 block 。

    关于objective-c - MKMapView 不刷新注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9170445/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com