butter_baby
butter_baby

Reputation: 888

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

I have a label that I would like to display on my map view, but the label should only display if the user zooms to a specific zoom level. So I'd like to do the following:

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

I like it to check and update the zoom level every time the user zooms. So I was thinking that ViewDidAppear would be the best place for this block of code.

Thanks in advance.

Upvotes: 0

Views: 165

Answers (1)

Apurv
Apurv

Reputation: 17186

You should handle it via below code.

-(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
    }
}

Upvotes: 1

Related Questions