我有一个mapView,其中我显示了一个位置(由自定义引脚表示),如屏幕截图所示
如何移动mapView以使图标完全可见?
答案 0 :(得分:0)
要实现此目的,您首先需要在屏幕视图上计算要滚动的点(在这种情况下,假设其20像素直到注释标记),然后您需要将此点隐蔽到“地图”位置,以便可以将地图中心移动到该位置;-)。以下是在 MKMapView委托方法中用 Swift 编写的代码,在其中使用了注释。
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let pinLocation = view.annotation.coordinate
let currentCoordinates = mapView.centerCoordinate // Temporary saved map current center position
// Temp set map center position to pin location
mapView.centerCoordinate = pinLocation
let viewCenter = self.view.center
let fakecenter = CGPoint(x: viewCenter.x, y: viewCenter.y - 20) // point just up to the center point
// convert calculetd point to map location co-ordinates
let coordinate: CLLocationCoordinate2D = mapView.convert(fakecenter, toCoordinateFrom: self.view)
// reset to previous potion so thet animation start from that
mapView.centerCoordinate = currentCoordinates
self.mapView.setCenter(coordinate, animated: true) // change the new center
}