2

我试图设置visibleMapRectMKMapView 对象的属性,但结果映射 rect 不是我所期望的。

这是我的代码:

NSLog(@"current size %f %f", mapView.visibleMapRect.size.width, mapView.visibleMapRect.size.height);
NSLog(@"target size %f %f", newBounds.size.width, newBounds.size.height);
mapView.visibleMapRect = newBounds;
NSLog(@"new size %f %f", mapView.visibleMapRect.size.width, mapView.visibleMapRect.size.height);

这是结果:

2013-01-15 19:21:25.440 MyApp[4216:14c03] current size 67108864.594672 46006272.643333
2013-01-15 19:21:25.441 MyApp[4216:14c03] target size 3066685.527175 2102356.690531
2013-01-15 19:21:25.442 MyApp[4216:14c03] new size 4194304.162631  2875392.126220

这是什么魔法?以及如何将精确的可见矩形设置为我的地图视图?

4

1 回答 1

1

感谢 Anna Karenina 的评论,我找到了答案。MKMapView setVisibleMapRect 方法以最大缩放级别显示矩形以适应输入矩形并显示每个像素的地图图块像素以保持图像看起来清晰。

所以我编写了这段代码来预测 MKMapRect,它将为输入 MKMapRect 显示。

- (MKMapRect)expectedMapRectForMapRect:(MKMapRect)mapRect inMapView:(MKMapView*)mapView
{
    CGFloat targetPointPerPixelRatio = MAX(MKMapRectGetWidth(mapRect) / CGRectGetWidth(mapView.bounds), MKMapRectGetHeight(mapRect) / CGRectGetHeight(mapView.bounds));
    CGFloat expextedPointPerPixelRatio = powf(2, ceilf(log2f(targetPointPerPixelRatio)));

    NSLog(@"expextedPointPerPixelRatio %f", expextedPointPerPixelRatio);
    MKMapRect expectedMapRect;
    expectedMapRect.size = MKMapSizeMake(CGRectGetWidth(mapView.bounds)*expextedPointPerPixelRatio, CGRectGetHeight(mapView.bounds)*expextedPointPerPixelRatio);
    expectedMapRect.origin = MKMapPointMake(MKMapRectGetMidX(mapRect) - expectedMapRect.size.width/2, MKMapRectGetMidY(mapRect) - expectedMapRect.size.height/2);

    expectedMapRect.origin.x = roundf(expectedMapRect.origin.x / expextedPointPerPixelRatio) * expextedPointPerPixelRatio;
    expectedMapRect.origin.y = roundf(expectedMapRect.origin.y / expextedPointPerPixelRatio) * expextedPointPerPixelRatio;
    return expectedMapRect;
}
于 2013-01-16T11:27:11.150 回答