在我移动或放大地图之前,不会调用viewForAnnotation。因此,在触摸地图之前,我的注释不会显示。知道为什么吗?
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];
}
}
答案 0 :(得分:2)
我使用[self performSelectorInBackground:@selector(loadEverything) withObject:self];
代替detachThreadSelector
解决了这个问题。该代码(在viewDidLoad中)最初未发布。
感谢您帮助所有人!