移动的MKMapView自动注释 - 动画呢? [英] MKMapView moving Annotations Automatically - animate them?

查看:200
本文介绍了移动的MKMapView自动注释 - 动画呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有注释,可以非常快速地更新的一组数据。此刻我型重构他们回来到地图上之前删除所有注释。

 的NSArray * existingpoints = [mapView.annotations filteredArrayUsing predicate:NS predicate predicateWithFormat:@(自isKindOfClass:%@)![ MKUserLocation类]]];
[图形页面removeAnnotations:existingpoints];

我估计他们是反正自定义对象的何处,所以想能够调用这个和移动注释不删除并重新添加回地图。这个例子叫我做这工作,我想几乎民意调查在下面。

   - (CLLocationCoordinate2D)坐标
{
    CLLocationCoordinate2D坐标;
    coord.latitude = [纬度的doubleValue];
    coord.longitude = [伦敦股票交易所的doubleValue];
        双differencetime = exampleTime;
        双speedmoving;
        双distanceTravelled = speedmoving * differencetime;        CLLocationDistance movedDistance = distanceTravelled;
        双radiansHeaded = DEG2RAD([self.heading的doubleValue]);
        CLLocation * newLocation = [passedLocation newLoc:沿movedDistance:radiansHeaded];
        坐标= newLocation.coordinate;    返回坐标;
}

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

 #进口<基金会/ Foundation.h>
#进口< CoreLocation / CoreLocation.h>
#进口< MapKit / MapKit.h>@interface的TestObject:NSObject的< MKAnnotation> {
    * NSString的adshex;
    * NSString的经纬度;
    * NSString的结肠;
    * NSString的称号;
    * NSString的字幕;
    CLLocationCoordinate2D协调;
}
@属性(非原子,保留)的NSString * adshex;
@属性(非原子,保留)的NSString *纬度;
@属性(非原子,保留)的NSString * LON;
@属性(非原子,保留)的NSString *称号;
@属性(非原子,保留)的NSString *字幕;
@属性(非原子,只读)CLLocationCoordinate2D协调;
- (CLLocationCoordinate2D)协调;@结束


解决方案

如果您更新注释的坐标使用setCoordinate方法(或同等学历),地图视图会自动更新视图上的标注的位置。 <一href=\"http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html\">This在文档页面说以下内容:


  

重要提示:当你实现
  协调物业在你的类,它
  建议您的合成
  创建。如果您选择实施
  此属性的方法
  自己,或者如果你手动修改
  变量的基本属性
  在后您的类的其他部分
  注释已被添加到地图,
  一定要送出去的键值
  观察(KVO)通知,当你
  做。地图套件使用志愿通知
  检测改变的坐标,
  标题和小标题的性能
  注释和进行必要的
  改变到地图显示。如果你这样做
  不发出通知的志愿,在
  注解的位置可能不
  可以在地图上正确地更新。


地图视图将只知道它是否告知(经由KVO)注释的重新读取坐标属性的坐标发生了变化。做到这一点的方法之一是实现setCoordinate方法和调用无论你有code,更新注释的位置。

在您的code,您要重新计算的只读属性协调自身坐标。你可以做的是将其添加到注释.m文件(并向.H):

   - (无效)setCoordinate:(CLLocationCoordinate2D)newCoordinate
{
    //没做什么
}

和在更新的地点,请致电setCoordinate方法上标注的地方:

  [someAnnotation setCoordinate:someAnnotation.coordinate];

您可以在您当前删除/重新添加注释的地方做到这一点。

以上调用看起来有趣,因为你必须在坐标getter方法​​的坐标重新计算。虽然这应作为一个速战速决/测试工作,我不建议经常使用它。

相反,你可以重新计算标注的位置之外(你现在删除/重新添加注释),并通过新的坐标setCoordinate。您的注释对象可以存储在您目前有(设置它们在setCoordinate,并且只使用这些构建一个CLLocationCoordinate2D从吸气返回)或(更好的)用坐标伊娃本身(设置在纬度/经度高德新位置setCoordinate并在吸气归还)。

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

解决方案

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

这篇关于移动的MKMapView自动注释 - 动画呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆