IOS update annotations on mapview

I have a mapview where the coordinates of annotations are constantly updated, but when I use setCoordinate, the annotation does not move. How to update annotations to reflect their coordinates?

- (void)updateUnits { PFQuery *query = [PFQuery queryWithClassName:@"devices"]; [query whereKeyExists:@"location"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (!error) { for (PFObject *row in objects) { PFGeoPoint *geoPoint = [row objectForKey:@"location"]; CLLocationCoordinate2D coord = { geoPoint.latitude, geoPoint.longitude }; for (id<MKAnnotation> ann in mapView.annotations) { if ([ann.title isEqualToString:[row objectForKey:@"deviceid"]]) { [ann setCoordinate:coord]; NSLog(@"latitude is: %f" , coord.latitude); NSLog(@"Is called"); break; } else { //[self.mapView removeAnnotation:ann]; } } } } else { } }]; } 
+8
source share
5 answers

Updated (to reflect the solution):

Having your own annotations and implementing setCoordinate and / or a synthesizing coordinate can cause problems.

Source: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html

Previous solution:

You can simply delete all annotations and then re-add them.

 [mapView removeAnnotations:[mapView.annotations]]; [mapView addAnnotations:(NSArray *)]; 

or delete them and again add them one at a time:

 for (id<MKAnnotation> annotation in mapView.annotations) { [mapView removeAnnotation:annotation]; // change coordinates etc [mapView addAnnotation:annotation]; } 
+18
source

Submit the annotation to the main thread, and then try changing the user interface in the background thread

 dispatch_async(dispatch_get_main_queue()) { self.mapView.addAnnotation(annotation) } 
+9
source

Swift 3.0 version of Nathan's answer (thanks Nathan):

 DispatchQueue.main.async { mapView.addAnnotation(annotation) } 

Lateral remark, Nathan's answer should have much more speed. I really need this help when deleting the annotation, the same problem exists and is fixed by sending the update to the main queue.

+6
source

Try using [self.mapView removeAnnotations:self.mapView.annotations] :

 - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self.mapView removeAnnotations:self.mapView.annotations]; PFQuery *query = [PFQuery queryWithClassName:@"caches"]; [query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) { for (PFObject *comment in comments) { NSString *tit = comment[@"titulo"]; NSString *lat = comment[@"latitud"]; NSString *lon = comment[@"longitud"]; float latFloat = [lat floatValue]; float lonFloat = [lon floatValue]; CLLocationCoordinate2D Pin; Pin.latitude = latFloat; Pin.longitude = lonFloat; MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc]init]; annotationPoint.coordinate = Pin; annotationPoint.title = tit; [self.mapView addAnnotation:annotationPoint]; } }]; } 
0
source

Just add

 mapView.reloadStyle(self) 
0
source

All Articles