iphone - MKMapView 自动移动注释 - 为它们设置动画?

标签 iphone annotations mkmapview mkannotationview

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

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

我计算它们在自定义对象中的位置,因此希望能够调用它并“移动”注释,而无需将其删除并重新添加回 map 。我进行的示例调用有效并且我想几乎“民意调查”如下。

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

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

#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

最佳答案

如果您使用 setCooperative 方法(或等效方法)更新注释的坐标, map View 将自动更新注释在 View 上的位置。 This page在文档中说了以下内容:

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.

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

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

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

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

[someAnnotation setCoordinate:someAnnotation.coordinate];

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

上面的调用看起来很有趣,因为您在坐标获取方法中重新计算了坐标。尽管它应该可以作为快速修复/测试,但我不建议定期使用它。

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

关于iphone - MKMapView 自动移动注释 - 为它们设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4113085/

相关文章:

java - 为IntelliJ中的所有参数添加@NotNull注释

ios - 如何创建符合 <MKAnnotation> 协议(protocol)的自定义类

iphone - Xcode 中多个目标的自动递增内部版本号

iphone - 在 iPhone 中实现 tesseract OCR

javascript - 如何创建可检测浏览器类型并进行相应重定向的 Google 协作平台脚本?

Android:android 编译时验证意向附加已通过

python - 是否可以通过 doctest 测试使用 get_type_hints 的函数?

ios - 点击时自定义标注在 MKMapView 中被取消

ios - mkmapview如何通过注解获取位置

iphone - MPMoviePlayerController 上的专辑封面