代码之家  ›  专栏  ›  技术社区  ›  Electron

在iOS 7地图相机旋转上更新地图注释

  •  5
  • Electron  · 技术社区  · 12 年前

    我正试图得到它,这样当你旋转iOS 7地图时,注释会随着相机的标题一起旋转。想象一下,我有必须始终指向北方的pin注释。

    这一开始看起来很简单,应该有一个MKMapViewDelegate来获取相机旋转,但实际上没有。

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

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

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

    到目前为止,我最成功的方法是添加一个旋转手势识别器来计算旋转增量,并将其用于在区域更改代理开始时报告的相机标题。这在一定程度上起到了作用,但在OS7中,你可以“轻弹”你的旋转手势,它会增加速度,而我似乎无法跟踪。有没有办法实时跟踪摄像机的航向?

    - (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];
    }
    
    1 回复  |  直到 12 年前
        1
  •  3
  •   Ross Kimes    12 年前

    不幸的是,苹果公司不会对任何地图信息进行实时更新。最好的办法是设置一个CADisplayLink,并在它发生变化时更新您需要的任何内容。像这样的。

    @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];
    }