Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MKAnnotation not visible *until* user drags map

I'm building an app where annotations mark points of interest that the user drives by. The map tracks the user's location and scrolls along, but annotations that should come into view on the map don't appear until the user drags the map with her finger.

Adding and removing annotations works fine so I'm not sure what code to show.

Has anyone seen similar behavior?

Edit:

This affects only some annotations - some appear, some don't. No apparent pattern.

As suggested, I've changed the code so that I add and remove annotations only on the main thread:

dispatch_async(dispatch_get_main_queue(), ^{
    [self.mapView removeAnnotations:annotationsToRemove];
    [self.mapView addAnnotations:annotationsToAdd];
});

But the problem persists. Darn...

like image 770
Gregor Avatar asked Jun 23 '14 19:06

Gregor


3 Answers

For me I just had to call addAnnotations on the main thread:

dispatch_async(dispatch_get_main_queue(), ^{
        [self.mapView addAnnotations:annotations];
    });

And it works fine

like image 179
Oluwatobi Omotayo Avatar answered Nov 15 '22 23:11

Oluwatobi Omotayo


when you add an annotation call all the following methods on that annotation for it to show.

[_mapView addAnnotation:theAnnotation];
[_mapView deselectAnnotation:theAnnotation animated:NO];
[_mapView selectAnnotation:theAnnotation animated:NO];

For some of the apps I have worked on, unless you deselect and select the annotation, it wouldn't show it on the map (even if you just add it). Hope this helps!

like image 2
Nitin Alabur Avatar answered Nov 15 '22 22:11

Nitin Alabur


It's hard to say without seeing any of your code. That being said, if you haven't already, try checking to see if any of the annotations are within the current visible map area in mapView:didUpdateUserLocation and, if they are, refresh your map view (see the answer to How do you check if an MKAnnotation is available within a MKCoordinateRegion for more on checking annotations against the visible map rect).

like image 1
jgarewal Avatar answered Nov 15 '22 22:11

jgarewal