簡體   English   中英

MKMapView的攝像頭旋轉時無法縮放

[英]MKMapView can't be zoom when its camera heading rotating

我想根據iOS設備的方向旋轉地圖視圖,實現方式如下

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

}

然后我遇到一個問題,當mapView的攝像機旋轉時,mapView無法縮放。

我怎么解決這個問題?

我意識到MKMapCameraMKMapCamera中的新類,也許是不穩定的。

最后,我以另一種方式實現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委托中實現地圖旋轉

#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)];
}

做完了!

您應該嘗試使用方法- (void)setUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated在初始化MKMapView時設置- (void)setUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated例如:

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

我的地圖確實按照此代碼的標題旋轉。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM