我有一个带有各种注释的 MKMapView。点击注释时,我希望地图尽可能地居中并放大该注释。
使用
mapView.setCenter
可以设置中心,但不能放大。
将
mapView.setRegion
与我定义的某些区域一起使用我想要的中心位置和跨度排序,但我设置的跨度很大程度上是任意的,当它太小时,似乎mapView默认为我想要的其他更大的跨度可以看到。
有没有办法让我以指定位置为中心并以编程方式尽可能放大该位置?
您可以将跨度设置为距中心位置精确米。试试这个:
private func zoomIn(location: CLLocationCoordinate2D, with metersFromCenter: Int = 0) {
let region = MKCoordinateRegion(center: location,
latitudinalMeters: CLLocationDistance(exactly: metersFromCenter)!,
longitudinalMeters: CLLocationDistance(exactly: metersFromCenter)!)
mapView.setRegion(mapView.regionThatFits(region), animated: true)
}
此处输出,这是最大缩放级别。 请注意,默认情况下,此跨度级别与 AppleMap 不进行数学计算。