我在使用 MKMapView 和 MKMapCamera 时注意到了奇怪的行为。我需要实现一些 Apple Maps 应用程序的标准行为。基本上,当 Map View 的用户跟踪模式设置为MKUserTrackingModeFollowWithHeading时,我需要能够在 2D 和 3D 模式之间切换。
目前,当 userTrackingMode 设置为MKUserTrackingModeNone时,我的解决方案工作正常,但它在其他模式下会出现奇怪的动画(无论我 setCamera:animated:YES 还是 NO,都会发生这种情况)。当我在设置新参数之前和之后记录相机参数时,唯一的区别是我的音高变化。
另一个问题是改变相机的高度以编程方式放大或缩小。我试图在 IBAction 方法中用捏合或拉出手势来模仿缩放。在followWithHeadingMode地图总是返回到“默认”缩放级别(相机的高度),而在其他模式下效果很好。
我启动了示例项目来检查发生了什么,这是我的单视图应用程序的视图控制器,带有地图视图,出现问题:
#import "CCViewController.h"
@interface CCViewController ()
@end
@implementation CCViewController
- (void)viewDidLoad{
[super viewDidLoad];
}
- (void)setUpMap{
[self.mapView setShowsUserLocation: YES];
}
- (void)viewDidAppear:(BOOL)animated{
[self setUpMap];
}
- (IBAction)mapModeSwitchClicked:(id)sender {
MKMapCamera *newCamera = [MKMapCamera camera];
newCamera.centerCoordinate = self.mapView.camera.centerCoordinate;
newCamera.heading = self.mapView.camera.heading;
newCamera.altitude = self.mapView.camera.altitude;
if(!self.mapView.camera.pitch){
newCamera.pitch = 50;
self.mapModeSwitch.title = @"2D";
}
else{
newCamera.pitch = 0;
self.mapModeSwitch.title = @"3D";
}
[self.mapView setCamera:newCamera animated:YES];
}
- (IBAction)locateMeButtonClicked:(id)sender {
if(self.mapView.userTrackingMode != MKUserTrackingModeFollowWithHeading)
[self.mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];
else
[self.mapView setUserTrackingMode:MKUserTrackingModeNone animated:YES];
}
- (void)zoomMapWithZoomFactor:(double)factor{
MKMapCamera *newCamera = [MKMapCamera camera];
newCamera.pitch = self.mapView.camera.pitch;
newCamera.heading = self.mapView.camera.heading;
newCamera.altitude = self.mapView.camera.altitude * factor;
newCamera.centerCoordinate = self.mapView.camera.centerCoordinate;
[self.mapView setCamera:newCamera animated:YES];
}
- (IBAction)zoomInClicked:(id)sender {
[self zoomMapWithZoomFactor: 0.5];
}
- (IBAction)zoomOutClicked:(id)sender {
[self zoomMapWithZoomFactor: 2];
}
@end
我正在寻找的是类似于 Apple Maps 应用程序的行为。你有什么想法?我究竟做错了什么 ?