MKMapView的visibleMapRect属性的奇怪行为 [英] Strange behavior of MKMapView visibleMapRect property

查看:1263
本文介绍了MKMapView的visibleMapRect属性的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置 visibleMapRect 属性MKMapView对象,但结果映射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]当前大小67108864.594672 46006272.643333 
2013-01-15 19:21:25.441 MyApp [4216:14c03] 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方法显示具有最大缩放级别的矩形以适合输入矩形,并显示地图瓦片每个像素的像素以保持图像看起来清晰。因此,我编写此代码以预测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;
}


I tried to set visibleMapRect property of MKMapView object but result map rect is not the exact what I expected.

This is my code:

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);

And this is the result:

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

What kind of magic is this? And how can I set the precise visible rect to my map view?

解决方案

Thanks to Anna Karenina's comment I found the answer. MKMapView setVisibleMapRect methods shows rect with maximum zoom level to fit input rect and show map tiles pixel per pixel to keep image looks crisp.

So I write this code to predict MKMapRect that will be shown for input 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;
}

这篇关于MKMapView的visibleMapRect属性的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆