MKMapView visibleMapRect属性的奇怪行为

我试图设置MKMapView对象的visibleMapRect属性,但结果映射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 

这有什么神奇之处? 如何在地图视图中设置精确的可见矩形?

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

所以我编写这段代码来预测将为输入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; }