How to move MKMapView based on selected annotation

后端 未结 4 1768
甜味超标
甜味超标 2021-01-02 03:43

I have an MKMapView which populates the entirety of my view, but when a pin is selected, I am sliding up another view on top of the map. I want to move the map so the pin wi

4条回答
  •  感情败类
    2021-01-02 04:25

    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

提交回复
热议问题