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.