iOS refresh annotations on mapview

2019-02-09 12:02发布

I have a mapview where the annotation's coordinates are constantly being updated but when I use setCoordinate, the annotation does not move. How do I refresh the 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 {

        }
    }];
}

4条回答
Evening l夕情丶
2楼-- · 2019-02-09 12:12

Updated (to reflect the solution):

Having your own custom annotations and implementing setCoordinate and/or synthesizing coordinate may cause issues.

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

Previous solution:

You can simply remove all of the annotations and then re-add them.

[mapView removeAnnotations:[mapView.annotations]];

[mapView addAnnotations:(NSArray *)];

or remove them and re-add them one by one:

for (id<MKAnnotation> annotation in mapView.annotations)
{
    [mapView removeAnnotation:annotation];
    // change coordinates etc
    [mapView addAnnotation:annotation]; 
}
查看更多
放荡不羁爱自由
3楼-- · 2019-02-09 12:12

Swift 3.0 version of Nathan's answer (thank you Nathan):

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

Side note, Nathan's answer should have far more upvotes. I actually needed this help with removing an annotation, the same issue exists and is fixed by dispatching the update to the main queue.

查看更多
beautiful°
4楼-- · 2019-02-09 12:15

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];

     }

 }];
}
查看更多
乱世女痞
5楼-- · 2019-02-09 12:18

Dispatch the add annotation to the main thread rather then attempt to modify the UI on a background thread

dispatch_async(dispatch_get_main_queue()) {
    self.mapView.addAnnotation(annotation)
}
查看更多
登录 后发表回答