ran*_*dom 1 xcode annotations objective-c mkmapview ios
我有一个MKMapView(显然),它显示了用户周围的住房位置.
我有一个Radius工具,在进行选择时,应根据用户周围的距离添加/删除注释.
我有它添加/删除罚款,但由于某种原因,注释将不会显示,直到我放大或缩小.
这是根据距离添加/删除注释的方法.我尝试了两种不同的方法.
将新注释添加到数组,然后通过添加到地图[mapView addAnnotations:NSArray].
添加使用时找到的注释[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];
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我还尝试了上述方法的结尾:
[self._mapView setNeedsDisplay];
[self._mapView setNeedsLayout];
Run Code Online (Sandbox Code Playgroud)
此外,强制刷新(看到它可能工作的地方):
self._mapView.showsUserLocation = NO;
self._mapView.showsUserLocation = YES;
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感谢,并一如既往,感谢您花时间阅读.
Joh*_*pia 10
我猜这updateBasedDistance:是从后台线程调用的.检查NSLog(@"Am I in the UI thread? %d", [NSThread isMainThread]);.如果是0,那么你应该移动removeAnnotations:,并addAnnotation:以一个performSelectorOnMainThread:调用,或在主线程GCD块.
| 归档时间: |
|
| 查看次数: |
3342 次 |
| 最近记录: |