简体   繁体   English

旋转MKMapView

[英]rotating MKMapView

I have a working app with an MKMapView showing user location. 我有一个可以显示用户位置的MKMapView应用程序。 So far, so good: 到现在为止还挺好:

- (void)viewDidLoad {
   ...
   [myMapView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
   [myMapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
}

Now, I allow interface rotations: 现在,我允许界面旋转:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return YES;
}

If I now turn the device and center to the user location 如果我现在将设备转到中心并转到用户位置

   [myMapView setCenterCoordinate:[myMapView userLocation].coordinate animated:YES];

The mapView will rotate as expected, but the user location will not be centered, but on the bottom of the screen (distance from top stays the same as in portrait mode). mapView将按预期旋转,但用户位置不会居中,而是位于屏幕底部(与顶部的距离与纵向模式下相同)。

I expect it to be centered, though... 我希望它会居中,但是...

Any ideas? 有任何想法吗?

Try including height in the setAutoresizingMask: so that the map view's height will automatically change when the superview changes its height on rotation. 尝试在setAutoresizingMask:包含高度setAutoresizingMask:这样,当setAutoresizingMask:视图在旋转时更改其高度时,地图视图的高度将自动更改。 Right now, only the width is resizing and so the y position of the map's center doesn't change. 目前,只有宽度正在调整大小,因此地图中心的y位置不会改变。

So this line: 所以这行:

[myMapView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

should be: 应该:

[myMapView setAutoresizingMask: 
    (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];


Additionally, you shouldn't need to manually center the map on the user location if you are setting the tracking mode to MKUserTrackingModeFollow (the map will do it automatically). 此外,如果您将跟踪模式设置为MKUserTrackingModeFollow ,则无需在用户位置上手动将地图MKUserTrackingModeFollow (地图会自动执行)。 You may have tried manually centering it to fix the height issue but fixing the autoresizing mask should eliminate the need for the explicit centering. 您可能已经尝试过手动对中以解决高度问题,但是修复自动调整大小蒙版应该消除了显式对中的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM