如何以特定缩放级别在 MKMapView 上显示 UILabel?

How can I display a UILabel on MKMapView at a specific zoom level?

我有一个标签,我想在我的地图视图上显示,但标签应该只在用户缩放到特定缩放级别时显示。所以我想做以下事情:

if (mapView.camera.altitude >= 5) {
     //display label here
}

我喜欢它在每次用户缩放时检查和更新缩放级别。所以我认为 ViewDidAppear 将是这段代码的最佳位置。

提前致谢。

你应该通过下面的代码来处理它。

-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    NSUInteger zoomLevel = MAXIMUM_ZOOM; // MAXIMUM_ZOOM is 20 with MapKit
    MKZoomScale zoomScale = mapView.visibleMapRect.size.width / mapView.frame.size.width; //MKZoomScale is just a CGFloat typedef
    double zoomExponent = log2(zoomScale);
    zoomLevel = (NSUInteger)(MAXIMUM_ZOOM - ceil(zoomExponent));
    if(zoomLevel > 5 && labelNotAdded)
    {
        //Add the label
    }
    else
    {
       //Remove the label
    }
}