我有一个mapView,其中显示了一个位置(由自定义图标表示),如屏幕截图所示:
![enter image description here](https://istack.dev59.com/mzeEe.webp)
如何移动mapView,以便完全可见图标?
我有一个mapView,其中显示了一个位置(由自定义图标表示),如屏幕截图所示:
如何移动mapView,以便完全可见图标?
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
}