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

发布于 2024-09-30 21:51:29 字数 1702 浏览 4 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只涨不跌 2024-10-07 21:51:29

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

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

仅当地图视图(通过 KVO)得知坐标已更改时,它才会知道重新读取注释的坐标属性。一种方法是实现 setCoordinate 方法,并在任何有更新注释位置的代码的地方调用该方法。

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

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

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

[someAnnotation setCoordinate:someAnnotation.coordinate];

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

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

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

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文