将MainViewController设置为MKMapView的委托时,不会出现注释

时间:2013-02-27 04:47:25

标签: ios delegates mkmapview mkannotation

我有一个我正在处理的时髦问题...我有一个非常基本的mkmapview,我将其拖入mainviewcontrollers视图。我有非常基本的注释,我在地图上显示。当我为mainviewcontroller创建mapview的委托(在IB或代码中)时,注释会停止显示。如果我不建立此连接,注释会按预期显示......任何想法?

#import "GDFlipsideViewController.h"
#import <MapKit/MapKit.h>

@interface GDMainViewController : UIViewController <MKMapViewDelegate>

@property (strong, nonatomic) IBOutlet UIToolbar *bottomToolbar;
@property (strong, nonatomic) IBOutlet MKMapView *theMap;
@property (strong, nonatomic) MKLocalSearchResponse *results;
@property (strong, nonatomic) MKLocalSearchRequest *request;
@property (strong, nonatomic) MKLocalSearch *search;

- (IBAction)locateHelp:(UIBarButtonItem *)sender;

@end
...

@implementation GDMainViewController
...
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    static NSString *AnnotationViewID = @"annotationViewID";
    GDHelpAnnotationView *annotationView = (GDHelpAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil){
        annotationView = [[GDHelpAnnotationView alloc]init];

    }

    annotationView.annotation = annotation;

    return annotationView;
}

...

- (IBAction)locateHelp:(UIBarButtonItem *)sender {

    [self searchForHelp];
}

..

- (void)searchForHelp {
    // drop pins for emergency destination
    [self searchForFireStations];
    [self searchForPoliceStations];
    [self searchForHospitals];

}

...

- (void)searchForFireStations {
    //Fire stations
    [_request setNaturalLanguageQuery:@"fire station"];
    [_request setRegion:[_theMap region]];
    _search = [[MKLocalSearch alloc]initWithRequest:_request];

    [_search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
        if ([response.mapItems count] == 0) {
            if ([response.mapItems count] == 0) {
                //will handle later
                return;
            }
        }

        [response.mapItems enumerateObjectsUsingBlock:^(MKMapItem *item, NSUInteger idx, BOOL *stop) {
            GDHelpAnnotation  *annotation = [[GDHelpAnnotation alloc] initWithPlacemark:item.placemark];
            annotation.title = item.name;
            annotation.subtitle = [item name];

            [_theMap addAnnotation:annotation]; // This gets called but doesnt show annotations when self is _theMap's delegate
        }];



    }];
}

1 个答案:

答案 0 :(得分:1)

如果在设置委托时调用了viewForAnnotation,但引脚没有出现,则错误可能在于GDHelpAnnotationView。您可以通过将viewForAnnotation替换为使用常规旧MKPinAnnotationView的更基本的- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation { static NSString *AnnotationViewID = @"annotationViewID"; MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; if (annotationView == nil) { annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID]; } else { annotationView.annotation = annotation; } return annotationView; } 来测试。

{{1}}