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

标签 ios mkmapview

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

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

但是我怎样才能使 map 以使这个 mapCenter 坐标位于 map 底部的点为中心?如果可能的话,无论 map 的初始缩放级别如何,我都希望它能正常工作。

最佳答案

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

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

附言- 如果您希望位置一直居中在底部,请通过添加该区域纬度跨度的一半(而不是四分之一)来创建新的 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];
}

Here's what you'd get with the method above.

关于iOS 设置 MKMapView 中心所以提供的位置在底部中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21437048/

相关文章:

ios - 如何在 Swift 中使用 MapKit 将叠加层粘贴到用户位置

ios - 将私有(private)应用程序提交到 App Store

iphone - Xcode 4.0.2/iOS SDK 4.3

ios - UIBezierPath 与核心数据的持久性

ios - 如何使用 swift 从 iPhone 联系人中检索电子邮件?

ios - iOS内购消耗品真的需要恢复吗?

ios - 在 MKUserTrackingModeFollowWithHeading map 模式下设置 MKMapCamera 时奇怪的 MKMapView 行为

iphone - 当用户位置已知或发生变化时,如何使 MKMapView map 在视觉上居中?

ios - MKMapView on Storyboard on multiple Views with just 1 instance

ios - 故障 map AnnotationView 行为