0

Having scanned all the similar problems and tried all possible solutions, I still can't find the solution to my case. I tried to put multiple pins on my MapView but it seems that my pin can't be added normally. I started a NSTimer in my viewDidLoad method so that every 5 seconds, some updated pins will be put on Map. I added debugging information and found the problem was the that the method viewForAnnotation is not getting called. (I've already set the delegate by calling [_map setDelegate:self])

My code is as following:

    - (void)viewDidLoad
   {
        [super viewDidLoad];
        _map = [[MKMapView alloc] initWithFrame:[[self view] frame]];
        [_map setDelegate:self];

        CLLocationCoordinate2D startLocation;
        startLocation.latitude = [startLat floatValue];
        startLocation.longitude = [startLong floatValue];
        MKCoordinateSpan span = MKCoordinateSpanMake(0.002, 0.002);
        MKCoordinateRegion region = MKCoordinateRegionMake(startLocation, span);
        [_map setRegion:region];

        [[self view] addSubview:_map];

        [self getPlacesForLocation:[_map centerCoordinate]];

        NSTimer *currentTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self  selector:@selector(theActionMethod:) userInfo:nil repeats:YES];
        [currentTimer fire];
   }

The getPlacesForLocationMethod:

-(void)getPlacesForLocation:(CLLocationCoordinate2D)location
{
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^
                   {

                       /*  get data from the website 
                       **  get the geo information and put them in the MapPin struct
                       */

                       [self putPinsOnMap];
                   }
}

putPinsOnMap:

-(void)putPinsOnMap
{

    for(Pinfo *iter in [_PinfoArray copy])
    {
        MapPin *pin = [[MapPin alloc] init];
        [pin setTitle:[iter from_user_name]];
        [pin setSubtitle:[iter text]];
        [pin setCoordinate:[iter location]];

        //NSLog(@"The coordinate is %f  %f", [pin coordinate].latitude, [pin coordinate].longitude);

        [_map addAnnotation:pin];

        /****************************************/

        NSLog(@"Try to put the pin on Map");
        /****************************************/
    }
}

And here is the content of my MapPin.h:

@interface MapPin : NSObject<MKAnnotation>

@property (nonatomic, copy) NSString *title, *subtitle;
@property (nonatomic) CLLocationCoordinate2D coordinate;

@end

If I stay in place, then every time the (void)putPinsOnMap gets called, it prints "Try to put pin on Map" but the viewForAnnotation method is not getting called (I also added debugging info there but none of them are printed). Only a few times, the method viewForAnnotation sometimes will be called if I zoom out to a great extent.

4

1 回答 1

1

您说您将调试信息添加到 viewForAnnotation,但您没有将 MKMapView 子类化以覆盖 viewForAnnotation 方法(或者至少,您正在分配一个 MKMapView,而不是任何子类)。我认为您正在寻找委托方法:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;

如果您在视图控制器中实现该方法(您正在制作 MKMapView 的委托),您可能会获得有用的信息。

如果 mapView:viewForAnnotation: 仍然没有被调用,除非你缩小了,那么你可能错误地放置了你的注释,它们只是在地球上的其他地方,而不是你最初在屏幕上的地方。

于 2013-03-18T21:46:51.500 回答