Ios6 MKMapView设置区域不精确 起始情况

Ios6 MKMapView设置区域不精确 起始情况,ios6,zooming,mapkit,Ios6,Zooming,Mapkit,iOS6,苹果地图。我在MKMapView上有两个注释。两个注释的坐标对于纬度分量具有相同的值,但是不同的经度 我想要什么 现在,我希望缩放地图,以便一个注释正好位于地图视图框架的左边缘,另一个注释位于框架的右边缘。为此,我尝试了MKMapView的setRegion和setVisibleMapRect方法 问题 问题是,这些方法似乎捕捉到特定的缩放级别,因此没有按照我的需要精确设置区域。我看到许多关于堆栈溢出的问题指出,这种行为在iOS5及以下版本中是正常的。但是,由于Apple Maps使用

iOS6,苹果地图。我在MKMapView上有两个注释。两个注释的坐标对于纬度分量具有相同的值,但是不同的经度

我想要什么 现在,我希望缩放地图,以便一个注释正好位于地图视图框架的左边缘,另一个注释位于框架的右边缘。为此,我尝试了MKMapView的setRegion和setVisibleMapRect方法

问题 问题是,这些方法似乎捕捉到特定的缩放级别,因此没有按照我的需要精确设置区域。我看到许多关于堆栈溢出的问题指出,这种行为在iOS5及以下版本中是正常的。但是,由于Apple Maps使用矢量图形,因此视图不受特定缩放级别的限制,无法以适当的分辨率显示图像

测试在。。。 我在iPhone4、4S、5、iPad3和iPadMini(都带有iOS6.1)上进行了测试,并在iOS6.1的模拟器上进行了测试

我的问题 那么,为什么setRegion和setVisibleMapRect会捕捉到特定的缩放级别,而不会根据我传递的值调整确切的区域

示例代码 在视图中,我将4个不同的位置定义为IVAR,并设置地图视图:

// define coords
_coord1 = CLLocationCoordinate2DMake(46.0, 13.0);
_coord2 = CLLocationCoordinate2DMake(46.0, 13.1);
_coord3 = CLLocationCoordinate2DMake(46.0, 13.2);
_coord4 = CLLocationCoordinate2DMake(46.0, 13.3);

// define frame for map in landscape mode
CGRect mainScreen = [[UIScreen mainScreen] bounds];
CGRect newSRect =  mainScreen;
newSRect.size.width = mainScreen.size.height;
newSRect.size.height = mainScreen.size.width;

// setup map view
customMapView = [[MKMapView alloc] initWithFrame:newSRect];
[customMapView setDelegate:self];
[customMapView setShowsUserLocation:NO];
[self.view addSubview:customMapView];
然后我添加了3个按钮。这些都会触发相同的addAnnotationsWithCoord1:coord2方法。第一个按钮通过_coord1和_coord2,第二个按钮通过_coord1和_coord3,第三个按钮通过_coord1和_coord4。方法如下所示(TapAnnotation是我的MKAnnotation子类):

然后,我计算两个坐标之间的中心点,并将地图的中心坐标设置为它(记住,由于所有坐标都具有相同的纬度,以下计算应该是正确的):

现在,我尝试设置地图的区域,使其符合我的要求:

CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude];
CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude];
CLLocationDistance meterSpanLong = [loc1 distanceFromLocation:loc2];
CLLocationDistance meterSpanLat = 0.1;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center, meterSpanLat, meterSpanLong);
[customMapView setRegion:region animated:NO];
这不符合预期,因此我尝试以下方法:

MKCoordinateSpan span = MKCoordinateSpanMake(0.01, northEast.longitude-southWest.longitude);
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[customMapView setRegion:region animated:NO];
这仍然没有达到预期效果,因此我尝试使用setVisibleMapRect:

MKMapPoint westPoint = MKMapPointForCoordinate(southWest);
MKMapPoint eastPoint = MKMapPointForCoordinate(northEast);
MKMapRect mapRect = MKMapRectMake(westPoint.x, westPoint.y,eastPoint.x-westPoint.x,1);
[customMapView setVisibleMapRect:mapRect animated:NO];
但是,它的行为并不像我想要的那样。作为验证,我计算从左注释到mapView框架左边缘的点距离:

// log the distance from the soutwest point to the left edge of the mapFrame
CGPoint tappedWestPoint = [customMapView convertCoordinate:southWest toPointToView:customMapView];
NSLog(@"distance: %f", tappedWestPoint.x);
  • 对于_coord1和_coord2,它显示:138
  • 对于_coord1和_coord3,它显示:138
  • 对于_coord1和_coord4,它显示:65
那么为什么我要得到这些值呢如果有任何结果符合预期,则这些值都应为0。


感谢您的帮助,我们已经为这个问题奋斗了一周。

请阅读
setRegion
上的文档:

设置此属性时,贴图可能会调整新的“区域”值,使其精确地适合贴图的可见区域。这是正常的,这样做是为了确保此属性中的值始终反映贴图的可见部分。但是,这确实意味着,如果您在设置该属性权限后获得该属性权限的值,则返回的值可能与您设置的值不匹配。(可以使用RegionAtFits:方法确定地图实际设置的区域。)


如果你想要精确的缩放,你必须自己算出数学

谢谢你。我确实在文档中读到了这一点,很抱歉在我的描述中没有提到这一点。问题是,在完成数学运算后,如何应用计算出的区域而不使mapView捕捉回缩放级别?再次感谢。@icanus医生大错特错了。setRegion在iphone4上不起作用,如前所述,它仍然会捕捉到提供的区域所在的下一个缩放级别。但这并不是提供的确切区域。doc声称只更正了纵横比,这是不正确的。“捕捉到下一个缩放级别”仍然适用(尽管现在有矢量贴图时有点过时),您解决了吗?我以前解决过这个问题,但现在它不再起作用了。
MKCoordinateSpan span = MKCoordinateSpanMake(0.01, northEast.longitude-southWest.longitude);
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[customMapView setRegion:region animated:NO];
MKMapPoint westPoint = MKMapPointForCoordinate(southWest);
MKMapPoint eastPoint = MKMapPointForCoordinate(northEast);
MKMapRect mapRect = MKMapRectMake(westPoint.x, westPoint.y,eastPoint.x-westPoint.x,1);
[customMapView setVisibleMapRect:mapRect animated:NO];
// log the distance from the soutwest point to the left edge of the mapFrame
CGPoint tappedWestPoint = [customMapView convertCoordinate:southWest toPointToView:customMapView];
NSLog(@"distance: %f", tappedWestPoint.x);