viewForAnnotation Not Called Until Map is Moved
viewForAnnotation is not being called until I move or zoom in on the map. Because of this, my annotations do not show until the map i开发者_开发问答s touched. Any idea why?
NSString *incident;
for (incident in weekFeed) {
NSString *finalCoordinates = [[NSString alloc] initWithFormat:@"%@", [incident valueForKey:@"coordinates"]];
NSArray *coordinatesArray = [finalCoordinates componentsSeparatedByString:@","];
latcoord = (@"%@", [coordinatesArray objectAtIndex:0]);
longcoord = (@"%@", [coordinatesArray objectAtIndex:1]);
// Final Logs
NSLog(@"Coordinates in NSString: [%@] - [%@]", latcoord, longcoord);
CLLocationCoordinate2D coord;
coord.latitude = [latcoord doubleValue];
coord.longitude = [longcoord doubleValue];
DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = [NSString stringWithFormat: @"%@", [incident valueForKey:@"incident_type"]];
ann.subtitle = [NSString stringWithFormat: @"%@", [incident valueForKey:@"note"]];
ann.coordinate = coord;
[mapView addAnnotation:ann];
[ann release];
}
// Custom Map Markers
-(MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil; //return nil to use default blue dot view
static NSString *AnnotationViewID = @"annotationViewID";
MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil) {
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
annotationView.canShowCallout = YES;
if ([annotationView.annotation.title isEqualToString:@"one"]) {
UIImage *pinImage = [UIImage imageNamed:@"marker_1.png"];
[annotationView setImage:pinImage];
}
if ([annotationView.annotation.title isEqualToString:@"two"]) {
UIImage *pinImage = [UIImage imageNamed:@"marker_2.png"];
[annotationView setImage:pinImage];
}
annotationView.annotation = annotation;
return annotationView;
}
- (void) mapView:(MKMapView *)mapV didAddAnnotationViews:(NSArray *)views {
CGRect visibleRect = [mapV annotationVisibleRect];
for (MKAnnotationView *view in views) {
CGRect endFrame = view.frame;
CGRect startFrame = endFrame; startFrame.origin.y = visibleRect.origin.y - startFrame.size.height;
view.frame = startFrame;
[UIView beginAnimations:@"drop" context:NULL];
[UIView setAnimationDuration:0.4];
view.frame = endFrame;
[UIView commitAnimations];
}
}
I solved it by using [self performSelectorInBackground:@selector(loadEverything) withObject:self];
instead of detachThreadSelector
. That code (in viewDidLoad) wasn't originally posted.
Thanks for helping everyone!
精彩评论