Centering MKMapView on spot N-pixels below pin

前端 未结 8 1424
梦毁少年i
梦毁少年i 2020-11-30 02:34

Want to center MKMapView on a point N-pixels below a given pin (which may or may not be visible in the current MapRect).

I\'ve been trying

8条回答
  •  迷失自我
    2020-11-30 02:49

    after reading this thread ad playing around, especially with the zooming on annotation, I ended up with following procedures:

    ** Centering on annotation:**

    - (void) centerOnSelection:(id)annotation
    {
        MKCoordinateRegion region = self.mapView.region;
        region.center = annotation.coordinate;
    
        CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
        region.center.latitude -= self.mapView.region.span.latitudeDelta * per;
    
        [self.mapView setRegion:region animated:YES];
    }
    

    ** Zooming on annotation:**

    - (void) zoomAndCenterOnSelection:(id)annotation
    {
        DLog(@"zoomAndCenterOnSelection");
    
        MKCoordinateRegion region = self.mapView.region;
        MKCoordinateSpan span = MKCoordinateSpanMake(0.005, 0.005);
    
        region.center = annotation.coordinate;
    
        CGFloat per = ([self sizeOfBottom] - [self sizeOfTop]) / (2 * self.mapView.frame.size.height);
        region.center.latitude -= self.mapView.region.span.latitudeDelta * span.latitudeDelta / region.span.latitudeDelta * per;
    
        region.span = span;
    
        [self.mapView setRegion:region animated:YES];
    }
    

    -(CGFloat) sizeOfBottom and -(CGFloat) sizeOfTop both return height of panels covering the mapview from the layout guides

提交回复
热议问题