Zoom in a MKMapView programmatically

后端 未结 9 1668
逝去的感伤
逝去的感伤 2020-12-04 13:27

I\'m using a MKMapView inside an iPhone app. When I click a button the zoom level must increase. This is my first approach:

MKCoordinateRegion z         


        
相关标签:
9条回答
  • 2020-12-04 14:17

    Here is my way to move map to the annotation point and zoom pretty close to it. You can easily change the zoom in line CGFloat newLatDelta = 0.06f;

    - (void)moveMapToAnnotation:(MKPointAnnotation*)annotation
    {
        CGFloat fractionLatLon = map.region.span.latitudeDelta / map.region.span.longitudeDelta;
        CGFloat newLatDelta = 0.06f;
        CGFloat newLonDelta = newLatDelta * fractionLatLon;
        MKCoordinateRegion region = MKCoordinateRegionMake(annotation.coordinate, MKCoordinateSpanMake(newLatDelta, newLonDelta));
        [map setRegion:region animated:YES];
    }
    
    0 讨论(0)
  • - (IBAction)btnZoomInPressed
    {
        MKCoordinateRegion region;
        MKCoordinateSpan span;
        region.center.latitude = lati;
        region.center.longitude = longi;
        span.latitudeDelta=viewMapSingleVenue.region.span.latitudeDelta /2.0002;
        span.longitudeDelta=viewMapSingleVenue.region.span.longitudeDelta /2.0002;
        region.span=span;
        [viewMapSingleVenue setRegion:region animated:TRUE];
    }
    
    - (IBAction)btnZoomOutPressed
    {
        MKCoordinateRegion region;
        MKCoordinateSpan span;
        region.center.latitude = lati;
        region.center.longitude = longi;
        span.latitudeDelta=viewMapSingleVenue.region.span.latitudeDelta *2;
        span.longitudeDelta=viewMapSingleVenue.region.span.longitudeDelta *2;
        if(span.latitudeDelta < 200)
        {
        region.span=span;
        [viewMapSingleVenue setRegion:region animated:TRUE];
        }
    }
    
    0 讨论(0)
  • 2020-12-04 14:23

    In Swift 4.2

    let location = mapView.userLocation
    let region = MKCoordinateRegion(center: location.coordinate, span: MKCoordinateSpan(latitudeDelta: 50, longitudeDelta: 50))
    mapView.setRegion(region, animated: true)
    
    0 讨论(0)
提交回复
热议问题