viewForAnnotation 在地图移动之前不会被调用
在我移动或放大地图之前,不会调用 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];
}
}
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 is 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];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过使用
[self PerformSelectorInBackground:@selector(loadEverything) withObject:self];
而不是detachThreadSelector
解决了这个问题。该代码(在 viewDidLoad 中)最初并未发布。感谢大家的帮助!
I solved it by using
[self performSelectorInBackground:@selector(loadEverything) withObject:self];
instead ofdetachThreadSelector
. That code (in viewDidLoad) wasn't originally posted.Thanks for helping everyone!