当部分地图被覆盖时居中 MKMapView
Center MKMapView when part of map is covered
我有 MKMapView,它从底部被另一个视图覆盖。假设地图的高度为 250,但从底部算起有 100 被其他视图覆盖。
现在,如果我使用 setRegion 将地图居中,它会像整个地图可见一样将地图居中,但我需要将它居中到真正可见的区域,即剩余的 150 高度。
你可以说,然后将地图的高度降低到150,这样它就不会被覆盖,但我需要通过设计来覆盖它,因为覆盖视图没有完全宽度到边界(有间隙从侧面)所以地图在覆盖视图周围可见。
那么,如何根据真实可见区域的高度对地图进行居中呢?
现在我正在使用这个:
CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(lat, long);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance (loc, 200, 200);
[_map setRegion:region animated:YES];
尝试使用:
[theMapView setVisibleMapRect:[theMapView mapRectThatFits:theMapRect]
animated:YES];
或者,如果您想进一步调整屏幕偏移量,您可以使用:
[theMapView setVisibleMapRect:[theMapView mapRectThatFits:theMapRect]
edgePadding:UIEdgeInsetsMake(50, 50, 50, 50)
animated:YES];
不确定您是否想过这个问题,但是 swift 中有一个解决方案。应该很容易适应 Obj-C
let pointYouWantCentered = CLLocationCoordinate2D(latitude: lat, longitude: lon)
pointYouWantCentered.latitude -= (mapView.region.span.latitudeDelta * 0.25)
mapView.setCenter(pointYouWantCentered, animated: true)
这将使屏幕上半部分的点居中。只要您知道底部视图占据的屏幕部分,您就可以将地图的中心调整到您想要的位置。例如,假设您有一个占据下半部分的视图,则将点置于屏幕上半部分的中心。如果您的底部视图占据了底部的 1/3,而您想将视图的顶部 2/3 居中,您可以执行以下操作。
let pointYouWantCentered = CLLocationCoordinate2D(latitude: lat, longitude: lon)
pointYouWantCentered.latitude -= (mapView.region.span.latitudeDelta * (1.0/6.0))
mapView.setCenter(pointYouWantCentered, animated: true)
希望对您有所帮助。
我有 MKMapView,它从底部被另一个视图覆盖。假设地图的高度为 250,但从底部算起有 100 被其他视图覆盖。
现在,如果我使用 setRegion 将地图居中,它会像整个地图可见一样将地图居中,但我需要将它居中到真正可见的区域,即剩余的 150 高度。
你可以说,然后将地图的高度降低到150,这样它就不会被覆盖,但我需要通过设计来覆盖它,因为覆盖视图没有完全宽度到边界(有间隙从侧面)所以地图在覆盖视图周围可见。
那么,如何根据真实可见区域的高度对地图进行居中呢?
现在我正在使用这个:
CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(lat, long);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance (loc, 200, 200);
[_map setRegion:region animated:YES];
尝试使用:
[theMapView setVisibleMapRect:[theMapView mapRectThatFits:theMapRect]
animated:YES];
或者,如果您想进一步调整屏幕偏移量,您可以使用:
[theMapView setVisibleMapRect:[theMapView mapRectThatFits:theMapRect]
edgePadding:UIEdgeInsetsMake(50, 50, 50, 50)
animated:YES];
不确定您是否想过这个问题,但是 swift 中有一个解决方案。应该很容易适应 Obj-C
let pointYouWantCentered = CLLocationCoordinate2D(latitude: lat, longitude: lon)
pointYouWantCentered.latitude -= (mapView.region.span.latitudeDelta * 0.25)
mapView.setCenter(pointYouWantCentered, animated: true)
这将使屏幕上半部分的点居中。只要您知道底部视图占据的屏幕部分,您就可以将地图的中心调整到您想要的位置。例如,假设您有一个占据下半部分的视图,则将点置于屏幕上半部分的中心。如果您的底部视图占据了底部的 1/3,而您想将视图的顶部 2/3 居中,您可以执行以下操作。
let pointYouWantCentered = CLLocationCoordinate2D(latitude: lat, longitude: lon)
pointYouWantCentered.latitude -= (mapView.region.span.latitudeDelta * (1.0/6.0))
mapView.setCenter(pointYouWantCentered, animated: true)
希望对您有所帮助。