iOS 设置 MKMapView 相机航向围绕 anchor 旋转

标签 ios mkmapview

我有一个 MKMapView,我试图围绕其自身的底部中心旋转。我有一个简单的方法来获取用户磁力计航向并将 map 的相机 View 更改为航向,这工作得很好。

但我的问题是在 map 上以纬度/经度设置 map 将围绕其旋转的 anchor 。我可以使用此方法将我想要的点设置为 map 的底部:

-(void)setMapCenterFromLocation:(CLLocationCoordinate2D)location {
    MKCoordinateRegion oldRegion = [self.mapView regionThatFits:MKCoordinateRegionMakeWithDistance(location, 10000, 10000)];
    CLLocationCoordinate2D centerPointOfOldRegion = oldRegion.center;

    CLLocationCoordinate2D centerPointOfNewRegion = CLLocationCoordinate2DMake(centerPointOfOldRegion.latitude + oldRegion.span.latitudeDelta / 2.0, centerPointOfOldRegion.longitude);

    MKCoordinateRegion newRegion = MKCoordinateRegionMake(centerPointOfNewRegion, oldRegion.span);

    [self.mapView setRegion:newRegion animated:YES];
}

我可以旋转 map :

- (void)magneticDirectionChanged:(NSNotification *)notification {
    CLHeading *heading = (CLHeading *)[[notification userInfo] valueForKey:@"heading"];

    CLLocationDirection newDirection = heading.trueHeading;

    self.currentHeading = (int)newDirection;

    [self.mapView.camera setHeading:self.currentHeading];
}

但是旋转发生在 map 的中心,而不是我想要的底部中心点。即使我在旋转后调用 setMapCenterFromLocation:,它仍将其视为从北方看,因此它会偏向 map 的两侧。

如何在以编程方式旋转 map /调整 map 大小时将特定点保留在 map 的底部中心?

编辑:我认为一个解决方案是将 map 的高度加倍,然后隐藏 map 的下半部分不可见。这是如何实现的?

最佳答案

首先,在 Interface Builder 中,将 map View 固定在 ScrollView 中。然后将 map View 的高度设置为 1000 之类的值。 map View 的中心将位于屏幕底部。这适用于 View Controller 上的零自动布局约束。

对我来说关键是 ScrollView 部分。在我添加它之前,无论我在 IB 中将其设置为什么,我都无法让 map View 绘制高于 568(尽管更大的宽度有效)。我想 ScrollView 会强制超过限制高度的任何内容。

关于iOS 设置 MKMapView 相机航向围绕 anchor 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21453199/

相关文章:

ios - MKMapview 实时汽车跟踪

ios - 从 mapView 中删除一条折线

ios - 检测用户何时向左或向右滑动以在 WKWebView 上返回或返回

ios - 如何在 hidesBottomBarWhenPushed 为 "TRUE"时隐藏自定义标签栏按钮

ios - 从 Swift 中的外部类发送数据。音乐应用程序

iphone - MKPinAnnotationView 与 MKAnnotationView

ios - MapKit,使用按钮将图钉放在一个位置

ios - UIActionsheet Picker 中的日期和时间

iphone - SetVisibleMapRect总是去北冰洋

ios - Swift MKMapView Pin 注释问题