iOS 刷新 map View 上的注释

标签 ios annotations mkmapview mkannotation mkannotationview

我有一个 map View ,其中注释的坐标不断更新,但是当我使用 setCoordinate 时,注释不会移动。如何刷新注释以反射(reflect)其坐标?

- (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 {

        }
    }];
}

最佳答案

更新(以反射(reflect)解决方案):

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

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

以前的解决方案:

您可以简单地删除所有注释,然后重新添加它们。

[mapView removeAnnotations:[mapView.annotations]];

[mapView addAnnotations:(NSArray *)];

或删除它们并一一重新添加:

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

关于iOS 刷新 map View 上的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14131345/

相关文章:

java - 没有 Autowiring 注释的Spring注入(inject)

ios - MKMapView:如何将 MKUserLocationView(蓝点)置于最前面?

ios - 在 Swift 中使用自定义点注释时出现错误

ios - MKAnnotation Pin 不会拖动,即使设置为可拖动

ios - 加载 MKMapView 时崩溃

ios - 在 uiLabel 的下划线部分和链接带下划线的部分

r - 在 Plotly R 中为条形图添加注释

java - 我可以让 Java 5 忽略 @Override 错误吗?

ios - 同时关闭两个当前的 ModalViewController(MPMediaPickerController 和 UIViewController)

iphone - 如何在 objective-c 中拆分字符串