MKMapView 自动移动注释 - 为它们设置动画?
我有一个可以快速更新的注释数据集。目前,我删除了所有注释,然后将它们重新绘制回地图上。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 setCoordinate 方法(或等效方法)更新注释的坐标,地图视图将自动更新注释在视图上的位置。文档中的此页面内容如下:
仅当地图视图(通过 KVO)得知坐标已更改时,它才会知道重新读取注释的坐标属性。一种方法是实现 setCoordinate 方法,并在任何有更新注释位置的代码的地方调用该方法。
在您的代码中,您正在重新计算只读坐标属性本身的坐标。您可以做的是将其添加到注释 .m 文件(和 .h)中:
并在更新位置的位置,调用注释上的 setCooperative 方法:
您可以在当前删除/的位置执行此操作重新添加注释。
上面的调用看起来很有趣,因为您在坐标获取方法中重新计算了坐标。尽管它应该作为快速修复/测试,但我不建议定期使用它。
相反,您可以在外部(当前删除/重新添加注释的位置)重新计算注释的位置,并将新坐标传递给 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:
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):
and in the place where you update locations, call the setCoordinate method on the annotation:
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).