Cocoa touch 如何基于所选注释移动MKMapView

Cocoa touch 如何基于所选注释移动MKMapView,cocoa-touch,mkmapview,Cocoa Touch,Mkmapview,我有一个MKMapView,它填充了我的整个视图,但是当选择了一个pin时,我会在地图顶部向上滑动另一个视图。我想移动地图,使大头针出现在地图可见区域的中心 很难解释,但希望它有意义! 提前感谢。您可以尝试从地图视图的visibleMapRect获取MKMapRect,将注释的坐标转换为MKMapPoint,重置MKMapRect的原点,使MKMapPoint位于适当的位置,然后使用setVisibleMapRect:animated:将可见区域设置为新的MKMapRect 例如,如果要移动地图

我有一个MKMapView,它填充了我的整个视图,但是当选择了一个pin时,我会在地图顶部向上滑动另一个视图。我想移动地图,使大头针出现在地图可见区域的中心

很难解释,但希望它有意义!
提前感谢。

您可以尝试从地图视图的
visibleMapRect
获取MKMapRect,将注释的坐标转换为MKMapPoint,重置MKMapRect的原点,使MKMapPoint位于适当的位置,然后使用
setVisibleMapRect:animated:
将可见区域设置为新的MKMapRect

例如,如果要移动地图,使注释水平居中,垂直向下25%,可以执行以下操作:

MKMapRect r = [mapView visibleMapRect];
MKMapPoint pt = MKMapPointForCoordinate([annotation coordinate]);
r.origin.x = pt.x - r.size.width * 0.5;
r.origin.y = pt.y - r.size.height * 0.25;
[mapView setVisibleMapRect:r animated:YES]; 

我最终完成了以下程序:

**以注释为中心:**

- (void) centerOnSelection:(id<MKAnnotation>)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];
}
- (void) zoomAndCenterOnSelection:(id<MKAnnotation>)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];
}
-(void)centerOnSelection:(id)注释
{
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:区域动画:是];
}
**缩放注释:**

- (void) centerOnSelection:(id<MKAnnotation>)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];
}
- (void) zoomAndCenterOnSelection:(id<MKAnnotation>)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];
}
-(无效)zoomAndCenterOnSelection:(id)注释
{
DLog(“zoomAndCenterOnSelection”);
MKCoordinateRegion region=self.mapView.region;
MKCoordinateSpan=MKCoordinateSpan(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.latitudelta*span.latitudelta/region.span.latitudelta*per;
region.span=span;
[self.mapView setRegion:区域动画:是];
}

-(CGFloat)sizeOfBottom
-(CGFloat)sizeOfTop
使用MKMapViewDelegate方法从布局指南返回覆盖地图视图的面板高度:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

    // center the mapView on the selected pin
    let region = MKCoordinateRegion(center: view.annotation!.coordinate, span: mapView.region.span)
    mapView.setRegion(region, animated: true)
}

我根据这里的答案在Swift 4上构建了它

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

        // center the mapView on the selected pin
        var dest = view.annotation!.coordinate
        if view.annotation?.title == "Name of Annotation View"{//This is for move a little down depending on the MKAnnotationView size
            dest.latitude = dest.latitude + 0.002
        }
        let span = MKCoordinateSpan.init(latitudeDelta: 0.01, longitudeDelta: 0.01)
        let region = MKCoordinateRegion(center: dest, span: span)
        mapView.setRegion(region, animated: true)


    }