MKMapView 自动移动注释 - 为它们设置动画? [英] MKMapView moving Annotations Automatically - animate them?

查看:25
本文介绍了MKMapView 自动移动注释 - 为它们设置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以非常快速更新的注释数据集.目前我删除了所有注释,然后将它们重新绘制回地图上.

NSArray *existingpoints = [mapView.annotations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"!(self isKindOfClass: %@)", [MKUserLocation class]]];[mapView removeAnnotations:existingpoints];

我计算了它们在自定义对象中的位置,因此希望能够调用它并移动"注释,而无需将其删除并重新添加回地图.我所做的示例调用有效并且我想几乎轮询"如下.

- (CLLocationCoordinate2D) 坐标{CLLocationCoordinate2D 坐标;coord.latitude = [lat doubleValue];coord.longitude = [lon doubleValue];双差异时间 = 示例时间;双速移动;double distanceTravelled = speedmoving * differencetime;CLLocationDistance 移动距离 = distanceTravelled;double radiansHeaded = DEG2RAD([self.heading doubleValue]);CLLocation *newLocation = [passedLocation newLoc:movedDistance沿:radiansHeaded];coord = newLocation.coordinate;返回坐标;}

根据要求,对象的 .h 文件,我没有 SetCoordinate 方法..

#import #import #import @interface 测试对象:NSObject {NSString *adshex;NSString *lat;NSString *lon;NSString *title;NSString *副标题;CLLocationCoordinate2D 坐标;}@property(nonatomic,retain)NSString *adshex;@property(nonatomic,retain)NSString *lat;@property(nonatomic,retain)NSString *lon;@property(nonatomic,retain)NSString *title;@property(nonatomic,retain)NSString *subtitle;@property (nonatomic, readonly) CLLocationCoordinate2D 坐标;- (CLLocationCoordinate2D) 坐标;@结尾

解决方案

如果您使用 setCoordinate 方法(或等效方法)更新注释的坐标,地图视图将自动更新视图上注释的位置.此页面在文档中的说明如下:><块引用>

重要提示:当您实施在您的班级中协调属性,它建议你合成它创建.如果您选择实施此属性的方法自己,或者如果您手动修改该属性的基础变量在课程的其他部分注释已添加到地图中,一定要发出键值观察 (KVO) 通知时做.Map Kit 使用 KVO 通知来检测坐标的变化,标题和副标题属性注释并进行任何需要更改地图显示.如果你这样做不发送 KVO 通知,您的注释的位置可能不在地图上正确更新.

如果地图视图被告知(通过 KVO)坐标已更改,它只会知道重新读取注释的坐标属性.一种方法是实现一个 setCoordinate 方法,并在您拥有更新注释位置的代码的任何地方调用该方法.

在您的代码中,您正在重新计算只读坐标属性本身中的坐标.您可以做的是将其添加到注释 .m 文件(和 .h)中:

- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate{//没做什么}

并在更新位置的地方,调用注解上的 setCoordinate 方法:

[someAnnotation setCoordinate:someAnnotation.coordinate];

您可以在当前删除/重新添加注释的位置执行此操作.

上面的调用看起来很有趣,因为您在坐标获取方法中重新计算了坐标.虽然它应该作为快速修复/测试工作,但我不建议经常使用它.

相反,您可以在外部(您当前删除/重新添加注释的位置)重新计算注释的位置,并将新坐标传递给 setCoordinate.您的注释对象可以将其新位置存储在您当前拥有的 lat/lng 变量中(将它们设置在 setCoordinate 中并仅使用那些来构造 CLLocationCoordinate2D 以从 getter 返回)或(更好)使用坐标 ivar 本身(将其设置在setCoordinate 并在 getter 中返回).

I have a data set of annotations that can update very quickly. At the moment I remove all annotations before replotting them back onto the map.

NSArray *existingpoints = [mapView.annotations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"!(self isKindOfClass: %@)", [MKUserLocation class]]];
[mapView removeAnnotations:existingpoints];

I calculate where they are anyway within the custom object so would like to be able to call this and "move" the annotation without removing and re-adding it back to the map. The example call I make which works and I would like to almost "poll" is below.

- (CLLocationCoordinate2D) coordinate
{
    CLLocationCoordinate2D coord;
    coord.latitude = [lat doubleValue];
    coord.longitude = [lon doubleValue];


        double differencetime = exampleTime;
        double speedmoving;
        double distanceTravelled = speedmoving * differencetime;

        CLLocationDistance movedDistance = distanceTravelled;
        double radiansHeaded = DEG2RAD([self.heading doubleValue]);
        CLLocation *newLocation = [passedLocation newLoc:movedDistance along:radiansHeaded];
        coord = newLocation.coordinate;

    return coord;
}

As requested, the .h file of the Object, I do not have a SetCoordinate method..

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface TestObject : NSObject <MKAnnotation>{
    NSString *adshex;
    NSString *lat;
    NSString *lon;


    NSString *title;
    NSString *subtitle;


    CLLocationCoordinate2D coordinate;
}
@property(nonatomic,retain)NSString *adshex;
@property(nonatomic,retain)NSString *lat;
@property(nonatomic,retain)NSString *lon;


@property(nonatomic,retain)NSString *title;
@property(nonatomic,retain)NSString *subtitle;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;


- (CLLocationCoordinate2D) coordinate;

@end

解决方案

If you update the annotation's coordinate using a setCoordinate method (or equivalent), the map view will automatically update the position of the annotation on the view. This page in the docs says the following:

Important: When you implement the coordinate property in your class, it is recommended that you synthesize its creation. If you choose to implement the methods for this property yourself, or if you manually modify the variable underlying that property in other parts of your class after the annotation has been added to the map, be sure to send out key-value observing (KVO) notifications when you do. Map Kit uses KVO notifications to detect changes to the coordinate, title, and subtitle properties of your annotations and make any needed changes to the map display. If you do not send out KVO notifications, the position of your annotations may not be updated properly on the map.

The map view will only know to re-read the coordinate property of the annotation if it's told (via KVO) that the coordinate has changed. One way to do that is implement a setCoordinate method and call that wherever you have the code that updates the annotation's location.

In your code, you are re-calculating the coordinate in the readonly coordinate property itself. What you could do is add this to the annotation .m file (and to the .h):

- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate
{
    //do nothing
}

and in the place where you update locations, call the setCoordinate method on the annotation:

[someAnnotation setCoordinate:someAnnotation.coordinate];

You could do this in the place where you currently remove/re-add the annotations.

The above call looks funny because you have the coordinate re-calc in the coordinate-getter method. Although it should work as a quick fix/test, I don't recommend using it regularly.

Instead, you could re-calc the annotation's location outside (where you currently remove/re-add the annotations) and pass the new coordinate to setCoordinate. Your annotation object could store its new location in the lat/lng ivars you currently have (set them in the setCoordinate and use only those to construct a CLLocationCoordinate2D to return from the getter) or (better) use the coordinate ivar itself (set it in setCoordinate and return it in the getter).

这篇关于MKMapView 自动移动注释 - 为它们设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆