【问题标题】:Center an MKAnnotation in a specific MKMapRect在特定的 MKMapRect 中居中 MKAnnotation
【发布时间】:2016-06-01 06:26:39
【问题描述】:

我正在尝试在给定的 MKMapRect 中将注释/坐标居中。我首先创建了一个矩形,它是地图视图和另一个视图之间的可见区域。然后将此矩形转换为与地图视图成比例的矩形,以便正确显示。然后我将注释设置为显示在左上角,用于测试目的。

if let annotation = annotation {

    let visibleRect = CGRectMake(0, 0, CGRectGetWidth(mapView.frame), CGRectGetMinY(stackViewController.view.frame))
    let convertedRect = mapView.convertRect(visibleRect, toView: mapView)
    let convertedMapRect = MKMapRectMake(Double(convertedRect.origin.x), Double(convertedRect.origin.y), Double(convertedRect.width), Double(convertedRect.height))

    let point = MKMapPointForCoordinate(annotation.coordinate)
    let pointRect = MKMapRectMake((MKMapRectGetMidX(convertedMapRect) - point.x) / 2, (MKMapRectGetMidY(convertedMapRect) - point.y) / 2, convertedMapRect.size.width, convertedMapRect.size.height)


    mapView.setVisibleMapRect(pointRect, animated: true)
    mapView.selectAnnotation(annotation, animated: true)
}

此代码完美运行,并将按预期在左上角显示注释。但是,当我尝试在该矩形内将注释居中而不是将其放在左上角时,地图会以海为中心,而不是在纽约。这通常是因为该点无效。记录矩形后,当我执行中心计算时,它返回的 x 和 y 值太小。我认为这是因为像素数没有正确转换为地图的坐标系。这是为什么,我该如何扩展它?

if let annotation = annotation {

    let visibleRect = CGRectMake(0, 0, CGRectGetWidth(mapView.frame), CGRectGetMinY(stackViewController.view.frame))
    let convertedRect = mapView.convertRect(visibleRect, toView: mapView)
    let convertedMapRect = MKMapRectMake(Double(convertedRect.origin.x), Double(convertedRect.origin.y), Double(convertedRect.width), Double(convertedRect.height))

    let point = MKMapPointForCoordinate(annotation.coordinate)
    let pointRect = MKMapRectMake((MKMapRectGetMaxX(convertedMapRect) - point.x) / 2, (MKMapRectGetMaxX(convertedMapRect) - point.y) / 2, convertedMapRect.size.width, convertedMapRect.size.height)

    mapView.setVisibleMapRect(pointRect, animated: true)
    mapView.selectAnnotation(annotation, animated: true)
}

【问题讨论】:

    标签: ios swift mkmapview mapkit


    【解决方案1】:

    我在这里偶然发现了这篇文章,它对我有很大帮助:Centering MKMapView on spot N-pixels below pin

    如答案中所述,您应该通过将其从地图视图转换为具有或多或少标准化框架的视图控制器的视图来计算目标点。

    // define the rect in which to center the annotation
    let visibleRect = CGRectMake(0, 0, CGRectGetWidth(view.frame), 200)
    
    centerAnnotationInRect(someAnnotation, rect: visibleRect)
    
    func centerAnnotationInRect(annotation: MKAnnotation, rect: CGRect) {
    
        guard let mapView = mapView else {
            return
        }
    
        let visibleCenter = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect))
    
        let annotationCenter = mapView.convertCoordinate(annotation.coordinate, toPointToView: view)
    
        let distanceX: CGFloat = visibleCenter.x - annotationCenter.x
        let distanceY = visibleCenter.y - annotationCenter.y
    
        mapView.scrollWithOffset(CGPoint(x: distanceX, y: distanceY), animated: true)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-16
      • 1970-01-01
      • 2014-06-28
      相关资源
      最近更新 更多