MKMapCamera在旋转时不尊重地图框架

时间:2013-10-02 08:23:58

标签: ios mapkit ios7

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

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

似乎Apple在MKMapView中添加了额外的逻辑,以保持右下方的“Legal”标签,无论地图框架是什么,我认为这也导致了这个问题。

我是如何强制地图围绕地图底部而不是中心旋转的?

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

1 个答案:

答案 0 :(得分: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];

}