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

2024-05-15

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

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

但是我怎样才能使地图中心位于使该地图中心坐标放置在地图底部的点上呢?如果可能的话,我希望它能够工作,无论地图的初始缩放级别如何。


我给你写了一个快速方法,应该可以解决问题......

获得后MKCoordinateRegion以位置坐标为中心,您可以创建一个新的CLLocationCoordinate2D通过添加该区域纬度跨度的一小部分(在本例中为四分之一)来确定中心点。使用新的中心点和旧区域的跨度创建一个新的坐标区域,将其设置为region of the MKMapView,就可以开始了。

附注- 如果您希望位置始终在底部居中,请创建新的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];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐