iOS设置MKMapView中心,所以提供的位置是在底部的中心

我有一个MKMapView和一个永不改变的CLLocationCoordinate2D。 我想要做的是将地图居中放置,以便将此坐标放置在地图的底部中心。 我可以用这个简单的坐标将坐标集中在这个坐标上:

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(mapCenter, 10000, 10000); [self.mapView setRegion:viewRegion animated:YES]; 

但是,我怎样才能让地图集中在使地图中心坐标位于地图底部的那一点呢? 如果可能的话,我希望它能够工作,而不pipe地图的初始缩放级别如何。

我写了一个快速的方法,应该做的伎俩…

获得位置坐标居中的MKCoordinateRegion坐标区域后,可以通过添加该区域纬度跨度的一小部分(本例中为第四个)来创build新的CLLocationCoordinate2D坐标CLLocationCoordinate2D中心点。 使用新的中心点和旧区域的跨度创build一个新的坐标区域,将其设置为MKMapViewregion ,然后您就可以走了。

Ps – 如果您想要将位置居中置于底部,则可以通过添加区域纬度跨度的一半(而不是第四个)来创build新的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]; } 

这是你用上面的方法得到的。

太棒了马修! 谢谢!

SWIFT 3中的相同解决scheme:

 func setLocation(location: CLLocationCoordinate2D, inBottomCenterOfMapView: MKMapView) { let oldRegion = mapView.regionThatFits(MKCoordinateRegionMakeWithDistance(location, 800, 800)) let centerPointOfOldRegion = oldRegion.center let centerPointOfNewRegion = CLLocationCoordinate2DMake(centerPointOfOldRegion.latitude + oldRegion.span.latitudeDelta/4.0, centerPointOfOldRegion.longitude) let newRegion = MKCoordinateRegionMake(centerPointOfNewRegion, oldRegion.span) worldView.setRegion(newRegion, animated: true) }