viewForAnnotation 在地图移动之前不会被调用

发布于 2024-11-27 01:45:27 字数 2661 浏览 1 评论 0原文

在我移动或放大地图之前,不会调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染火枫林 2024-12-04 01:45:28

我通过使用[self PerformSelectorInBackground:@selector(loadEverything) withObject:self];而不是detachThreadSelector解决了这个问题。该代码(在 viewDidLoad 中)最初并未发布。

感谢大家的帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文