iOS设置MKMapView中心,因此提供的位置位于底部中心

shi*_*tix 4 mkmapview ios

我有一个MKMapView和一个永不改变的CLLocationCoordinate2D.我要做的是将地图居中,以便将此坐标放置在地图的底部中心.我可以使用简单的方法将地图置于此坐标中心:

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(mapCenter, 10000, 10000);
[self.mapView setRegion:viewRegion animated:YES];
Run Code Online (Sandbox Code Playgroud)

但是我怎样才能使地图以使得mapCenter坐标放置在地图底部的点为中心?如果可能的话,无论地图的初始缩放级别如何,我都希望它能够正常工作.

Mat*_*hew 8

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

得到一个后MKCoordinateRegion与坐标位置为中心,您可以创建一个新CLLocationCoordinate2D加入该地区的纬度跨度的一小部分中心点(在这种情况下,第四).使用新的中心点和旧区域的跨度创建一个新的坐标区域,将其设置为regionMKMapView,和你去好.

PS -如果你想在底部中间一路的位置,创造新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];
}
Run Code Online (Sandbox Code Playgroud)

以下是您使用上述方法获得的内容.