我在Path风格的视差表视图标题中显示MKMapView。要创建效果,mapView边界大于用户可见的区域。我需要设置地图视图区域,以便所有地图的注释都包含在MKMapView的可见矩形中。最好的方法是什么?
为了清晰起见编辑:这是一个用例。 mapView大小为320 x 380.但是,可见区域由rect(0.0,20.0,320.0,100.0)定义。我需要设置区域,以便所有注释都出现在mapView中的此rect中。
答案 0 :(得分:16)
设置地图区域以便所有注释都包含在MKMapView
的某个部分中,可以分三步完成。输入是mapView
和annotationsFrame
。
MKMapRect
mapRect
。mapView.bounds
和annotationsFrame
计算填充插入内容。-setVisibleMapRect:edgePadding:animated:
。以下是测试的屏幕截图。红色叠加层显示annotationsFrame
。
这是代码。 小心:这是一种简化将其添加到代码中的方法,而不会针对边缘情况进行测试,例如使用相同的坐标传入n个注释,或者具有相距太远的注释,地图必须缩小太多,或者坐标以+/- 180度经度跨越地图边缘。
- (void)zoomAnnotationsOnMapView:(MKMapView *)mapView toFrame:(CGRect)annotationsFrame animated:(BOOL)animated
{
if (_mapView.annotations.count < 2) return;
// Step 1: make an MKMapRect that contains all the annotations
NSArray *annotations = _mapView.annotations;
id <MKAnnotation> firstAnnotation = [annotations objectAtIndex:0];
MKMapPoint minPoint = MKMapPointForCoordinate(firstAnnotation.coordinate);
MKMapPoint maxPoint = minPoint;
for (id <MKAnnotation> annotation in annotations) {
MKMapPoint point = MKMapPointForCoordinate(annotation.coordinate);
if (point.x < minPoint.x) minPoint.x = point.x;
if (point.y < minPoint.y) minPoint.y = point.y;
if (point.x > maxPoint.x) maxPoint.x = point.x;
if (point.y > maxPoint.y) maxPoint.y = point.y;
}
MKMapRect mapRect = MKMapRectMake(minPoint.x, minPoint.y, maxPoint.x - minPoint.x, maxPoint.y - minPoint.y);
// Step 2: Calculate the edge padding
UIEdgeInsets edgePadding = UIEdgeInsetsMake(
CGRectGetMinY(annotationsFrame),
CGRectGetMinX(annotationsFrame),
CGRectGetMaxY(mapBounds) - CGRectGetMaxY(annotationsFrame),
CGRectGetMaxX(mapBounds) - CGRectGetMaxX(annotationsFrame)
);
// Step 3: Set the map rect
[mapView setVisibleMapRect:mapRect edgePadding:edgePadding animated:animated];
}
如果你想要一个完美的位置(谁没有),这里有三件事需要考虑:
annotationsFrame
,但注释本身可能在外面。为了防止这种情况,只需使用更多填充。例如,如果您的注释是20x20并且以坐标为中心,则在所有边上再使用10个填充。答案 1 :(得分:4)
从iOS 7.0开始,可以使用showAnnotations
轻松实现此目的。
夫特:
mapView.showAnnotations(mapView.annotations, animated: true)
目标-C:
[mapView showAnnotations:mapView.annotations animated:YES];
以上语句将调整地图视图的可见矩形以显示所有注释。
答案 2 :(得分:0)
如果您准备接近计算,可以使用一些巧妙的缩放来完成。
你的目标区域是高于80的mapView,即380.因此,您需要一个比计算的区域高4.75倍的区域以适合您的注释。 (上面额外增加0.25,下面增加3.5)。
首先,您需要获得一个区域(或maprect,您喜欢的工作方式)并使其与目标可视区域的比例相同。这是因为真正宽而短的区域不会触及可视区域的顶部和底部,因此乘以其高度不会触及地图视图的顶部和底部。因此,如果viewable_height/viewable_width > annotations_height/annotations_width
您应将annotations_height
设置为annotations_width * (viewable_height/viewable_width)
。
然后你可以在注释框的北边添加25%,在南边增加350%。您可以通过向中移动212.5%(当前高度)的中心并将垂直跨度增加475%来实现此目的。
现在,所有这些都是一个近似值,假设世界是球体,我们没有看平面投影(即赤道附近的1度纬度被绘制成小于极点附近的1度)。但是,如果你想要准确,你可以考虑根据纬度等来缩放数字。如果你只处理城市规模的注释,你可能会没事。
希望有所帮助。
答案 3 :(得分:0)
如果要查找给定矩形中的注释:
- (NSArray*)allAnnotationsInMapRect:(MKMapRect)mapRect {
NSMutableArray *annotationsInRect = [NSMutableArray array];
for(id<MKAnnotation *ann in self.allAnnotations) {
MKMapPoint pt = MKMapPointForCoordinate(ann.coordinate);
if(MKMapRectContainsPoint(mapRect, pt)) {
[annotationsInRect addObject:ann];
}
}
return annotationsInRect;
}
并确保注释VIEWS在rect中,获取注释的区域, 然后遍历它们并获取每个视图的边界,看看边界是否适合地图的visibleRect,如果没有修改该区域!
~~喜欢这个:
- (void)assureAnnotationViewsAreVisible:(NSArray*)annotations originalRegion:(MKCoordinateRegion)originalRegion {
CGFloat smallestX = MAXFLOAT;
CGFloat smallestY = MAXFLOAT;
CGFloat biggestX = -100;
CGFloat biggestY = -100;
//NSLog(@"---: %d", annotations.count);
for(id<MKAnnotation> *annotation in annotations) {
UIView *annotationView = [self.mapView viewForAnnotation:v];
CGRect annotationViewFrame = annotationView.bounds;
annotationViewFrame.origin = [self.mapView convertCoordinate:annotationView.coordinate toPointToView:self.mapView];
annotationViewFrame.origin = CGPointMake(annotationViewFrame.origin.x-annotationViewFrame.size.width/2,
annotationViewFrame.origin.y-annotationViewFrame.size.height);
smallestX = MIN(annotationViewFrame.origin.x, smallestX);
smallestY = MIN(annotationViewFrame.origin.y, smallestY);
biggestX = MAX(annotationViewFrame.origin.x+annotationViewFrame.size.width, biggestX);
biggestY = MAX(annotationViewFrame.origin.y+annotationViewFrame.size.height, biggestY);
}
//NSLog(@"---");
CGRect bounds = self.mapView.bounds;
if(smallestX < bounds.origin.x || smallestY < bounds.origin.y || biggestX > bounds.origin.x+bounds.size.width || biggestY > bounds.origin.y+bounds.size.height) {
CGRect neededRect = bounds;
neededRect.origin = CGPointMake(MIN(bounds.origin.x, smallestX), MIN(bounds.origin.y, smallestY));
neededRect.size = CGSizeMake(MAX(bounds.size.width, biggestX), MAX(bounds.size.height, biggestY));
MKCoordinateRegion neededRegion = [self.mapView convertRect:neededRect toRegionFromView:self.mapView];
_ignoreRegionChange = YES;
[self.mapView setRegion:originalRegion animated:NO];
_ignoreRegionChange = NO;
[self.mapView setRegion:neededRegion animated:YES];
}
else {
MKCoordinateRegion currentRegion = self.mapView.region;
_ignoreRegionChange = YES;
[self.mapView setRegion:originalRegion animated:NO];
_ignoreRegionChange = NO;
[self.mapView setRegion:currentRegion animated:YES];
}
}
答案 4 :(得分:0)
首先需要添加注释: (当然这是在你已经有一个注释列表之后)
Swift4:
self.mapView.addAnnotations(annotations)
let currentView = mapView.visibleMapRect
mapView.annotations(in: currentView)
您可以使用currentView常量或直接放置MKMapRect:下面:(。visibleMapRect返回:
&#34;地图视图当前显示的区域。&#34;
mapView.annotations(in: mapView.visibleMapRect)
答案 5 :(得分:0)
我发现一种更简单的方法,不计算是让地图视图计算它,然后我们调整边缘。
//1: Show all annotation on the map view, but without animation
self.mapView.showAnnotations(self.mapView.annotations, animated: false)
//2: Get the current visible map rect
let currentMapRect = self.mapView.visibleMapRect
//3: Create the edges inset that you want the map view to show all annotation within
let padding = UIEdgeInsets(top: 100, left: 100, bottom: 100, right: 100)
//4: Set current map rect but with new padding, also set animation to true to see effect
self.mapView.setVisibleMapRect(currentMapRect, edgePadding: padding, animated: true)
答案 6 :(得分:-1)
尝试从lan和lon的所有注释边缘值(最大值和最小值)中获取。
在开头定义此值:
static float maxLat = FLT_MIN;
static float maxLon = FLT_MIN;
static float minLat = FLT_MAX;
static float minLon = FLT_MAX;
然后使用此函数计算跨度和区域:
- (void) zoomAndFit {
for(int i = 0; i < [self.points count]; i++) {
PRPlaceModel *place = [self.points objectAtIndex:i];
CLLocationCoordinate2D location;
location.latitude = [place.lat floatValue];
location.longitude = [place.lon floatValue];
minLat = MIN(minLat, location.latitude);
minLon = MIN(minLon, location.longitude);
maxLat = MAX(maxLat, location.latitude);
maxLon = MAX(maxLon, location.longitude);
}
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 1.2*(maxLat - minLat);
span.longitudeDelta = 1.2*(maxLon - minLon);
CLLocationCoordinate2D location;
location.latitude = (minLat + maxLat)/2;
location.longitude = (minLon + maxLon)/2;
region.span=span;
region.center=location;
[self.mapView setRegion:region animated:TRUE];
[self.mapView regionThatFits:region];
}
并在viewDidLoad方法中使用它:
-(void)viewDidLoad
{
[super viewDidLoad];
[self zoomAndFit];
}