1

我有这个代码:

// MapViewController.h
#import <Foundation/Foundation.h>
#import "MyCLController.h"
#import <MapKit/MapKit.h>

@class ShowsContainerController;

@interface MapViewController : UIViewController <MyCLControllerDelegate,MKMapViewDelegate> {
    MyCLController *locationController;
    MKMapView *mapView;
}

- (void)locationUpdate:(CLLocation *)location;
- (void)locationError:(NSError *)error;

@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property (assign) BOOL updateMap;
@property (strong) ShowsContainerController *parent;

@end

// MapViewController.m
#import "MapViewController.h"
#import "ShowsContainerController.h"

#define METERS_PER_MILE 1609.344

@implementation MapViewController

@synthesize parent, mapView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.updateMap = YES;
    locationController = [[MyCLController alloc] init];
    locationController.delegate = self;
    [locationController.locationManager startUpdatingLocation];
    mapView.delegate = self;
}

- (void)locationUpdate:(CLLocation *)location {

    if( self.updateMap ){
        MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);

        [mapView setRegion:viewRegion animated:YES];

        self.parent = (ShowsContainerController *)self.parentViewController;

        [self.parent setLocation:location];

        self.updateMap = NO;
    }
}

- (void)locationError:(NSError *)error {
    NSLog([error localizedDescription]);
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    self.parent.mapReady = YES;

    if( self.parent.locationSet ){
        [self.parent callApi];
    }
}

#pragma mark MKMapViewDelegate

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    NSLog(@"delegating user location");
    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    NSLog(@"delegating pins");
    static NSString* ShowAnnotationIdentifier = @"showAnnotationIdentifier";

    MKPinAnnotationView* pinView = (MKPinAnnotationView *)
    [mapView dequeueReusableAnnotationViewWithIdentifier:ShowAnnotationIdentifier];

    if (!pinView)
    {
        // if an existing pin view was not available, create one
        MKPinAnnotationView* customPinView = [[MKPinAnnotationView alloc]
                                              initWithAnnotation:annotation reuseIdentifier:ShowAnnotationIdentifier];
        customPinView.enabled = YES;
        customPinView.pinColor = MKPinAnnotationColorRed;
        customPinView.animatesDrop = YES;
        customPinView.canShowCallout = YES;

        return customPinView;
    }
    else
    {
        pinView.annotation = annotation;
    }
    return pinView;
}

#pragma mark -

@end

这是添加我的别针的代码;在发出 api 调用之前它不会被触发,这是在viewDidAppear触发之后:

- (void)placeAnnotations:(NSArray *)shows
{

    NSEnumerator *e = [shows objectEnumerator];
    id show;
    NSMutableArray *annotations = [[NSMutableArray alloc] init];

    while (show = [e nextObject]) {
        double lat = [[[[[show objectForKey:@"venue"] objectForKey:@"location"] objectForKey:@"geo:point"] objectForKey:@"geo:lat"] doubleValue];
        double lng = [[[[[show objectForKey:@"venue"] objectForKey:@"location"] objectForKey:@"geo:point"] objectForKey:@"geo:long"] doubleValue];

        ShowAnnotation *annotation = [[ShowAnnotation alloc]
                                      initWithCoordinate:CLLocationCoordinate2DMake(lat, lng)
                                      withMap:self.mapController.mapView
                                      withTitle:[show objectForKey:@"title"]
                                      withSubtitle:[[show objectForKey:@"venue"] objectForKey:@"name" ]];
        [annotations addObject:annotation];
    }

    NSLog(@"adding annotations");
    [self.mapController.mapView addAnnotations:annotations];

}

日志显示“委派用户位置”,但从不显示“委派引脚”。它甚至不会触发viewForAnnotation用户位置的一个初始时间。

委托似乎正在工作,否则我将无法捕获此选择器,但为什么它只针对用户位置触发而不触发后续注释添加?

4

0 回答 0