3
votes

Edited to Add * I haven't found a solution for this one yet, can anyone please help? My problem is similar to this but the answer posted doesn't t work for me - MKMapView refresh after pin moves *End Edit

I have a search enabled map view. When user hits search, my custom annotations are added to the map view. The search returns 20 results with a 'load more' link. When I hit load more, more annotations should be added to map view.

The problem is - annotations gets added to the map view but is not displayed on the map. If I zoom in or zoom out to change the map region, the pins are shown.

I find that viewForAnnotations: is not getting called when i hit 'load more'. I am not sure how to trigger this. any one?

-(void)completedSearch:(NSNotification *)resultNotification
 {
   //begin loop
   //get coordinate
   customAnnotation *annot = [[customAnnotation alloc] initWithCoordinate:coordinate];
   //set title subtitle 
   [annotationsArray addObject:annot];
   //end loop
   [mView addAnnotations:annotationsArray];
   [mView setRegion:region animated:YES];
   [mView regionThatFits:region];
 }

  //custom annotation
  @interface customAnnotation : NSObject <MKAnnotation> 
 { 
   CLLocationCoordinate2D coordinate;  
   NSString*              title; 
   NSString*              info; 
 } 
 @property (nonatomic, retain) NSSting *title;
 @property (nonatomic, retain) NSSting *info;

 -(id) initWithCoordinate:(CLLocationCoordinate2D)c;
 @end

 @implementation customAnnotation
 @synthesize coordinate, title, info;
 -(id) initWithCoordinate:(CLLocationCoordinate2D)c
 {
   coordinate = c;
   return self;
 }
 -(void)dealloc{//dealloc stuff here}
 @end
3
I assume you are using - (void)addAnnotation:(id<MKAnnotation>)annotation to add more annotations? Let's see some code. - Felix Khazin
added code to show how i add the annotations - Dave
The docs say to add all your annotations immediately for performance reasons, so it may not even attempt to redraw until some event happens (like your scrolling). See if, after your add your annotations, calling setNeedsLayout on the map view forces it to re-layout its annotations (and call your delegate method immediately). - Jason Coco
Thanks, Jason. Tried it and no luck. This is really strange. If I move the map slightly the pins appear but just by clicking on load more, the pins don't appear. I called setNeedsLayout when I hit 'load more'. doesn't work :-( - Dave
Also, try calling setNeedsDisplay on the map view. Either way, the documentation clearly states to set all of your annotations when you configure the map. It obviously caches this information and doesn't update it until the map moves. setNeedsDisplay might force it to refresh, but if not... - Jason Coco

3 Answers

5
votes

just wanted to confirm i was adding annotations from a thread - which doesnt work. once I used this (addCameraIconOnMain adds my annoations)

[self performSelectorOnMainThread:@selector(addCameraIconOnMain:) withObject:cd waitUntilDone:true];    

its worked! thanks!

1
votes

This had something to do with the time lapse receiving the notification and then adding the annotation pins. Forcing it to use the main thread to add annotation pins worked.

0
votes

in my case the problem was I was adding the annotation before the map delegate. It must be something like:

- (void)viewDidLoad
{
    ...

   // map delegate
   [mView setDelegate:self];

   ...

   // add annotation
   [_map addAnnotation:_poiAnnotation];

   ...
}