MKMapView:使用当前缩放级别跟随当前位置

时间:2011-10-10 01:57:34

标签: iphone ios mkmapview core-location

我有一个方法可以移动地图,使其跟随当前用户位置。这工作正常,但目前缩放比例不是我想要的工作。我希望用户能够缩小或放大地图他们想要的方式,并且仍然按照用户刚刚在地图上设置的新缩放比例的当前位置。

我尝试执行以下操作,但效果不佳:

/**
 * Centers the map on the current location
 *
 * @version $Revision: 0.1
 */
- (void)centerMapOnLocation:(CLLocation *)location {

    // Make a region using our current zoom level
    CLLocationDistance latitude = mapView.region.span.latitudeDelta*100;
    CLLocationDistance longitude = mapView.region.span.longitudeDelta*100;
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location.coordinate, latitude, longitude);
    [mapView setRegion:region animated:YES];
}//end

1 个答案:

答案 0 :(得分:5)

MKMapView Zoom and Region描述了如何获取当前视图窗口。

当您创建新的MKCoordinateRegion以设置新视图时,请使用允许您指定大小的构造函数:

UIKIT_STATIC_INLINE MKCoordinateRegion MKCoordinateRegionMake(
   CLLocationCoordinate2D centerCoordinate,
   MKCoordinateSpan span
);

您需要传递来自当前区域的span和来自用户位置的centerCoordinate,我将为您提供此信息。

相关问题