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

时间:2010-11-06 12:36:18

标签: iphone annotations mkmapview mkannotationview

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

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

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

- (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文件,我没有SetCoordinate方法..

#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

1 个答案:

答案 0 :(得分:21)

如果使用setCoordinate方法(或等效方法)更新注释的坐标,则地图视图将自动更新注释在视图上的位置。文档中的This page说明如下:

  

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

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

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

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

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

[someAnnotation setCoordinate:someAnnotation.coordinate];

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

上面的调用看起来很有趣,因为你在coordinate-getter方法中有坐标重新计算。虽然它应该作为快速修复/测试工作,但我不建议定期使用它。

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

相关问题