【问题标题】:MKMapView can't be zoom when its camera heading rotatingMKMapView 在其相机航向旋转时无法缩放
【发布时间】:2016-02-24 17:54:42
【问题描述】:

我想根据iOS设备方向旋转我的地图视图,我实现如下

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    self.userDirection = newHeading.trueHeading;
    self.mapView.camera.heading = newHeading.trueHeading;

}

然后我遇到一个问题,当mapView的相机旋转时,mapView不能缩放。

我该如何解决这个问题?

【问题讨论】:

  • 我也遇到了同样的问题。有什么线索吗?
  • 是的,我有另一种实现 mapView 旋转的方法,我会尽快发布我的解决方案。

标签: ios mkmapview mapkit


【解决方案1】:

我意识到MKMapCamera是iOS7中的一个新类,可能是不稳定的。

最后,我以另一种方式实现 mapView 旋转:

首先,我放大mapView

#pragma mark - Map View Setup
- (void)setupMapView
{
    // setup size
    double edgeLength = sqrt(pow(screenSize.size.width, 2) + pow(screenSize.size.height, 2));
    self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, edgeLength, edgeLength)];
    self.mapView.center = screenCenter;
    self.mapView.delegate = self;
}

下一步,在CLLocationManager Delegate中实现地图旋转

#define DEGREES_TO_RADIANS(degrees) ((degrees / 180.0) * (M_PI))
...
...
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
     double radians = DEGREES_TO_RADIANS(newHeading.trueHeading);
     [self.mapView setTransform:CGAffineTransformMakeRotation(-radians)];
}

完成!

【讨论】:

    【解决方案2】:

    在初始化 MKMapView 时,您应该尝试使用 - (void)setUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated 方法,例如:

    self.mapView.delegate = self
    self.mapView.setUserTrackingMode(MKUserTrackingModeFollowWithHeading, animated: true)
    

    我的地图确实根据带有此代码的标题旋转。

    【讨论】:

    • 感谢您的回答。但是我已经尝试过你之前提到的这个解决方案,但是 mapView 在旋转时仍然可以缩放。
    【解决方案3】:

    在 MKMapView 的委托中,只需设置一个标志来检查用户是否正在尝试缩放:

    - (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
    {
      isTouching = YES;
    }
    

    更改后关闭:

    - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
    {
      isTouching = NO;
    }
    

    只有在不接触时才更改标题:

    - (void)changeMapHeading:(CLLocationDirection)heading
    {
      if (!isTouching)
      {
        targetMapView.camera.heading = heading;
      }
    }
    

    顺便说一句,您可能还想减少 LocationManager 中航向的更新频率:

    - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
    {
      NSDate *now = [NSDate date];
      NSTimeInterval diff = [now timeIntervalSinceDate:lastHeadingUpdateTime];
      if (diff > 0.1)
      {
        lastHeadingUpdateTime = now;
        [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationTrackingHeadingUpdated object:newHeading];
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      • 2014-01-06
      • 1970-01-01
      • 2011-02-22
      相关资源
      最近更新 更多