3

我有一个 mapView,我在其中显示了一个位置(由自定义图钉表示),如屏幕截图所示 在此处输入图像描述

如何移动 mapView 以使图标完全可见?

4

1 回答 1

1

要实现这一点,您首先需要计算要滚动的屏幕视图上的点(在您的情况下,假设它到注释标记的 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
}
于 2018-08-02T11:11:09.460 回答