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

Lee*_*ong 11 iphone annotations mkmapview mkannotationview

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

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

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

- (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;
}
Run Code Online (Sandbox Code Playgroud)

根据要求,Object的.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
Run Code Online (Sandbox Code Playgroud)

小智 21

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

要点:在类中实现coordinate属性时,建议您合成其创建.如果您选择自己实现此属性的方法,或者在将注释添加到地图后手动修改类的其他部分中该属性的变量,请务必发送键值观​​察(KVO)你这样做的通知.Map Kit使用KVO通知来检测注释的坐标,标题和字幕属性的更改,并对地图显示进行任何所需的更改.如果您不发送KVO通知,则可能无法在地图上正确更新注释的位置.

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

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

- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate
{
    //do nothing
}
Run Code Online (Sandbox Code Playgroud)

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

[someAnnotation setCoordinate:someAnnotation.coordinate];
Run Code Online (Sandbox Code Playgroud)

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

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

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