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 メソッドは、入力 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;
}
于 2013-01-16T11:27:11.150 に答える