• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - MKMapCamera 旋转时不尊重 map 框

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:11:33 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

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

任何想法如何强制 map 围绕 map 底部而不是中心旋转?

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



Best Answer-推荐答案


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

- (void)locationManagerCLLocationManager *)manager didUpdateHeadingCLHeading *)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];

}

关于ios - MKMapCamera 旋转时不尊重 map 框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19132799/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap