iOS设置了MKMapView中心,因此提供的位置在底部中心 [英] iOS set MKMapView center so supplied location is in the bottom center
问题描述
我有一个MKMapView和一个从未更改过的CLLocationCoordinate2D.我想做的是将地图居中,以便将此坐标放置在地图的底部中心.我可以使用以下简单的方法将地图定位在此坐标上:
I have a MKMapView and a never changing CLLocationCoordinate2D. What I am trying to do is center the map so that this coordinate will be placed at the bottom center of the map. I am able to center the map on this coordinate with the simple:
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(mapCenter, 10000, 10000);
[self.mapView setRegion:viewRegion animated:YES];
但是如何使地图居中于使该mapCenter坐标位于地图底部的点上呢?我希望它可以工作,无论地图的初始缩放级别如何.
But how can I make it so that the map centers on the point that would make this mapCenter coordinate be placed at the bottom of the map? I would like it to work regardless of the initial zoom level of the map if possible.
推荐答案
我写了一个快速的方法来解决这个问题...
I wrote you a quick method that should do the trick...
在获得位置坐标居中的MKCoordinateRegion
之后,可以通过添加该区域的纬度跨度的一部分(在本例中为四分之一)来创建新的CLLocationCoordinate2D
中心点.使用新的中心点和旧区域的跨度创建一个新的坐标区域,将其设置为MKMapView
的region
,您就可以开始了.
After getting a MKCoordinateRegion
with the location coordinate centered, you can create a new CLLocationCoordinate2D
center point by adding a fraction of that region's latitudinal span (in this case, a fourth). Create a new coordinate region using the new center point and the old region's span, set it as the region
of the MKMapView
, and you're good to go.
P.s. -如果您希望位置始终在底部居中,请通过添加该区域的纬度跨度的一半(而不是四分之一)来创建新的CLLocationCoordinate2D
中心点.
P.s. - If you want the location centered all the way at the bottom, create the new CLLocationCoordinate2D
center point by adding half of the region's latitudinal span (instead of a fourth).
-(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中心,因此提供的位置在底部中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!