iOS刷新mapview上的注释

前端之家收集整理的这篇文章主要介绍了iOS刷新mapview上的注释前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个mapview,其中注释的坐标不断更新,但是当我使用setCoordinate时,注释不会移动.如何刷新注释以反映其坐标?
- (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 {

        }
    }];
}

解决方法

更新(以反映解决方案):

拥有自己的自定义注释并实现setCoordinate和/或合成坐标可能会导致问题.

资料来源:@L_404_0@

以前的方案:

您只需删除所有注释,然后重新添加它们即可.

[mapView removeAnnotations:[mapView.annotations]];

[mapView addAnnotations:(NSArray *)];

删除它们并逐个重新添加

for (id<MKAnnotation> annotation in mapView.annotations)
{
    [mapView removeAnnotation:annotation];
    // change coordinates etc
    [mapView addAnnotation:annotation]; 
}
原文链接:https://www.f2er.com/iOS/333108.html

猜你在找的iOS相关文章