我有一个 MKMapView 和一个永不改变的 CLLocationCoordinate2D。我想要做的是将 map 居中,以便该坐标将放置在 map 的底部中心。我可以使用简单的方法将 map 置于此坐标的中心:
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(mapCenter, 10000, 10000);
[self.mapView setRegion:viewRegion animated:YES];
但是我怎样才能使 map 以使这个 mapCenter 坐标位于 map 底部的点为中心?如果可能的话,无论 map 的初始缩放级别如何,我都希望它能正常工作。
最佳答案
我给你写了一个应该可以解决问题的快速方法...
在获得以位置坐标为中心的 MKCoordinateRegion
之后,您可以通过添加该区域的纬度跨度的一部分(在本例中为第四)。使用新的中心点和旧区域的跨度创建一个新的坐标区域,将其设置为 MKMapView
的 region
,就可以了。
附言- 如果您希望位置一直居中在底部,请通过添加该区域纬度跨度的一半(而不是四分之一)来创建新的 CLLocationCoordinate2D
中心点。
-(void)setLocation:(CLLocationCoordinate2D)location inBottomCenterOfMapView:(MKMapView*)mapView
{
//Get the region (with the location centered) and the center point of that region
MKCoordinateRegion oldRegion = [mapView regionThatFits:MKCoordinateRegionMakeWithDistance(location, 800, 800)];
CLLocationCoordinate2D centerPointOfOldRegion = oldRegion.center;
//Create a new center point (I added a quarter of oldRegion's latitudinal span)
CLLocationCoordinate2D centerPointOfNewRegion = CLLocationCoordinate2DMake(centerPointOfOldRegion.latitude + oldRegion.span.latitudeDelta/4.0, centerPointOfOldRegion.longitude);
//Create a new region with the new center point (same span as oldRegion)
MKCoordinateRegion newRegion = MKCoordinateRegionMake(centerPointOfNewRegion, oldRegion.span);
//Set the mapView's region
[worldView setRegion:newRegion animated:YES];
}
关于iOS 设置 MKMapView 中心所以提供的位置在底部中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21437048/