2

我正在使用简单的 NSTimer 以编程方式旋转 MKMapView,以不断增加 MKMapCamera 的标题属性,该属性按预期工作(导致地图围绕华盛顿纪念碑缓慢旋转)。

我不想让地图围绕其中心旋转,而是希望它围绕地图底部旋转。解决方案应该很简单,将地图的高度加倍,然后围绕其中心旋转,该中心现在位于屏幕底部。在将地图高度加倍后,它仍然围绕屏幕中心而不是地图框的中心旋转。

苹果似乎在 MKMapView 中添加了额外的逻辑,以保持右下角的“法律”标签,无论地图框是什么,我认为这也是导致此问题的原因。

有什么想法可以强制地图围绕地图底部而不是中心旋转吗?

- (void)setupMap {

    // Works as expected (rotates around center of screen)
    CGRect mapFrame = self.view.bounds; // works as expected

    // Doesn't work as expected (also rotates around the center of the screen)
    //mapFrame.size.height = self.view.frame.size.height*2;

    // Create/show MKMapView
    testMapView = [[MKMapView alloc] initWithFrame:mapFrame];
    [self.view addSubview:testMapView];

    // Zoom into the Washington Monument with a pitch of 60°
    MKMapCamera *aCamera = [MKMapCamera camera];
    [aCamera setCenterCoordinate:CLLocationCoordinate2DMake(38.8895, -77.0352)];
    [aCamera setAltitude:400];
    [aCamera setPitch:60];
    [testMapView setCamera:aCamera];

    // Begin rotating map every 1/10th of a second
    NSTimer *aTimer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(rotateMap) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:aTimer forMode:NSDefaultRunLoopMode];
}

- (void)rotateMap {
    MKMapCamera *aCamera = [testMapView camera];
    [aCamera setHeading:aCamera.heading+1];
    [testMapView setCamera:aCamera];
}
4

1 回答 1

1

我意识到这是一个较老的问题,但这是一个非常令人沮丧的问题。Autolayout 和 MapKit 在幕后做了一些时髦的事情。我注意到 mapView 渲染会自动将我的地图视图居中以将用户位置放在屏幕中心。在我这样做之前,没有多少偏移、约束、变换、超级视图可以使地图不在屏幕中心。

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    CLLocationDegrees heading = newHeading.trueHeading >= 0 ? newHeading.trueHeading : newHeading.magneticHeading;

    //ACCOUNT FOR LANDSCAPE ORIENTATION IN HEADING
    if(self.interfaceOrientation == UIInterfaceOrientationLandscapeRight){
              heading += 180;
              if(heading > 360) heading -= 360;
    }

    //OFFSET MAP
    CGPoint p = [_mapView convertCoordinate:_currentLocation toPointToView:_mapView];
    CGPoint p2 = CGPointMake(p.x, p.y - MAP_VERTICAL_OFFSET);
    CLLocationCoordinate2D t = [_mapView convertPoint:p2 toCoordinateFromView:_mapView];

    [_aCamera setHeading:heading];
    [_aCamera setCenterCoordinate:t];
    [_mapView setCamera:_aCamera];

}
于 2014-01-23T21:53:20.730 回答