在iOS 7地图相机旋转上更新地图注释 [英] Update map annotations on iOS 7 map camera rotation

查看:80
本文介绍了在iOS 7地图相机旋转上更新地图注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取它,以便在旋转iOS 7地图时注解与相机标题一起旋转.想象一下,我的引脚注释必须始终指向北.

I am trying to get it so that when you rotate an iOS 7 map that the annotations rotate along with the camera heading. Imagine I had pin annotations that must point to North at all times.

这乍看起来似乎很简单,应该有一个MKMapViewDelegate来使摄像机旋转,但没有.

This would seem simple at first, there should be a MKMapViewDelegate for getting the camera rotation but there isn't.

我尝试使用地图委托来查询地图视图的camera.heading对象,但首先,这些委托似乎只在旋转手势之前和之后被调用一次:

I've tried using the map delegates to then query the map view's camera.heading object but firstly these delegates only seem to be called once before and once after a rotation gesture:

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

我还尝试在camera.heading对象上使用KVO,但这不起作用,并且camera对象似乎是某种代理对象,只有在完成旋转手势后才会更新.

I also tried using KVO on the camera.heading object but this doesn't work and the camera object seems to be some kind of proxy object that only updates once the rotation gesture is complete.

到目前为止,我最成功的方法是添加旋转手势识别器来计算旋转增量,并将其与在区域更改委托开始时报告的摄像机航向一起使用.这可以工作到一定程度,但是在OS 7中,您可以轻拂"您的旋转手势,并且它增加了我似乎无法跟踪的速度.有什么方法可以实时跟踪摄像机的前进方向?

My most successful method so far is to add a rotation gesture recogniser to calculate a rotation delta and use this with the camera heading reported at the beginning of the region change delegate. This works to a point but in OS 7 you can 'flick' your rotation gesture and it adds velocity which I can't seem to track. Is there any way to track the camera heading in real-time?

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
    heading = self.mapView.camera.heading;
}

- (void)rotationHandler:(UIRotationGestureRecognizer *)gesture
{
    if(gesture.state == UIGestureRecognizerStateChanged) {

        CGFloat headingDelta = (gesture.rotation * (180.0/M_PI) );
        headingDelta = fmod(headingDelta, 360.0);

        CGFloat newHeading = heading - headingDelta;

        [self updateCompassesWithHeading:actualHeading];        
    }
}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    [self updateCompassesWithHeading:self.mapView.camera.heading];
}

推荐答案

不幸的是,Apple并未提供任何地图信息的实时更新.最好的选择是设置CADisplayLink并在更改时更新所需的内容.像这样的东西.

Apple does not give real time updates to any map information unfortunately. Your best bet is to set up a CADisplayLink and update whatever you need to when it changes. Something like this.

@property (nonatomic) CLLocationDirection *previousHeading;
@property (nonatomic, strong) CADisplayLink *displayLink;


- (void)setUpDisplayLink
{
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkFired:)];

    [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}


- (void)displayLinkFired:(id)sender
{
   double difference = ABS(self.previousHeading - self.mapView.camera.heading);

   if (difference < .001)
       return;

   self.previousHeading = self.mapView.camera.heading;

   [self updateCompassesWithHeading:self.previousHeading];
}

这篇关于在iOS 7地图相机旋转上更新地图注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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