我的问题与此one有某种关联。但是对于iOS7,我想知道是否有更简单的方法。
所以基本上我有MKMapView
处理视差效果,就像Foursquare应用程序一样。
那么如何缩放MKMapView
可见矩形上的所有注释?
现在我有一个工作解决方案,它使用一些值来抵消我的注释的纬度,但我觉得这很奇怪。
详情:
为了给地图一个视差效果(当你在UITableView
上滚动时,地图也在滚动),你需要保留地图的一部分。在我的情况下,frame
的{{1}}为MKMapView
,其可见框架为(0 -120; 320 390)
。所以这里的目标是能够在可见的(0 0, 320 150);
上显示注释(放大)。
答案 0 :(得分:1)
这样的东西应该可行,但我还没有尝试过代码:
MKAnnotation* firstAnnotaiton = annotations[0];
CLLocationDegrees north = firstAnnotation.coordinate.latitude;
CLLocationDegrees south = firstAnnotation.coordinate.latitude;
CLLocationDegrees east = firstAnnotation.coordinate.longitude;
CLLocationDegrees west = firstAnnotation.coordinate.longitude;
for (int i = 1; i < annotations.count; i++)
{
MKAnnotation* annotation = annotations[i];
if (north < firstAnnotation.coordinate.latitude)
north = firstAnnotation.coordinate.latitude;
if (south > firstAnnotation.coordinate.latitude)
south = firstAnnotation.coordinate.latitude;
if (west < firstAnnotation.coordinate.longitude)
west = firstAnnotation.coordinate.longitude;
if (east > firstAnnotation.coordinate.longitude)
east = firstAnnotation.coordinate.longitude;
}
MKMapPoint upperLeft = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
north,
west
));
MKMapPoint lowerRight = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
south,
east
));
MKMapRect rect = MKMapRectMake(
upperLeft.x,
upperLeft.y,
lowerRight.x - upperLeft.x,
lowerRight.y - upperLeft.y
);
rect = [self.mapView mapRectThatFits:rect edgePadding:self.boundsInsets];
[self.mapView setVisibleMapRect:rect animated:animated];
此处的关键是让self.boundsInsets
为UIEdgeInsets
,指定视图实际大小与其可见边界之间的差异。