iOS在mapview上刷新注释

iOS refresh annotations on mapview(iOS在mapview上刷新注释)
本文介绍了iOS在mapview上刷新注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个地图视图,其中注释的坐标不断更新,但是当我使用 setCoordinate 时,注释不会移动.如何刷新注释以反映它们的坐标?

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 {

        }
    }];
}

推荐答案

更新(反映解决方案):

Updated (to reflect the solution):

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

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

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

以前的解决方案:

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

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

这篇关于iOS在mapview上刷新注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to calculate distance from user location to annotation when user moves(用户移动时如何计算用户位置到注释的距离)
In which case that mapView:viewForAnnotation: will be called?(在这种情况下 mapView:viewForAnnotation: 会被调用吗?)
Why does a custom MKMapView annotation image disappear on touch?(为什么自定义的 MKMapView 注释图像在触摸时会消失?)
What#39;s the meaning of new @SystemApi annotation, any difference from @hide?(新的@SystemApi 注解是什么意思,和@hide 有什么区别?)
Retrofit @body with @multipart having Issue(使用 @multipart 改造 @body 有问题)
iPhone Map Kit cluster pinpoints(iPhone Map Kit 集群定位)