在特定的 MKMapRect 中居中 MKAnnotation
Center an MKAnnotation in a specific MKMapRect
我试图在给定的 MKMapRect 中将 annotation/coordinate 居中。我首先创建了 rect,它是 mapview 和另一个视图之间的可见区域。然后将该矩形转换为与地图视图成比例的矩形,以便正确显示。然后我将注释设置为显示在左上角,用于测试目的。
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)
}
此代码运行良好,将按预期在左上角显示注释。但是,当我尝试将注释置于该矩形内而不是将其置于左上角时,地图将以大海为中心,而不是纽约。发生这种情况通常是因为点无效。记录了矩形后,当我执行中心计算时,它 returns 的 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)
}
我在这里偶然发现了这个 post,这对我有很大帮助: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)
}
我试图在给定的 MKMapRect 中将 annotation/coordinate 居中。我首先创建了 rect,它是 mapview 和另一个视图之间的可见区域。然后将该矩形转换为与地图视图成比例的矩形,以便正确显示。然后我将注释设置为显示在左上角,用于测试目的。
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)
}
此代码运行良好,将按预期在左上角显示注释。但是,当我尝试将注释置于该矩形内而不是将其置于左上角时,地图将以大海为中心,而不是纽约。发生这种情况通常是因为点无效。记录了矩形后,当我执行中心计算时,它 returns 的 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)
}
我在这里偶然发现了这个 post,这对我有很大帮助: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)
}