的MKMapView不刷新注解 [英] MKMapView not refreshing annotations
问题描述
我有一个的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.
-
添加新的注释到一个数组,然后通过
添加到地图[MapView的addAnnotations:NSArray的。
添加注解,因为它使用它们找到 [图形页面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;
任何帮助将非常AP preciated和往常一样,感谢您抽出时间来阅读。
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不刷新注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!