私は Swift 3 と Xcode 8 を使用した IOS 開発アプリの初心者です。私のアプリでは、regionDidChangeAnimated デリゲート メソッドを使用して、zoomLevel を mapView.region.span.latitudeDelta * 111.000 でメートル単位で保持します。
新しい位置データが利用可能になったら、MKMapCamera を使用して、以前に計算してインスタンス変数に保存した fromDistance パラメータ = mapView.region.span.latitudeDelta * 111.000 で userlocation を表示します。
問題は、ズーム レベルをピンチ インおよびピンチ アウトすると正しく機能しないことです。
以下にビットコードを投稿します。
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
mapChangedFromUserInteraction = mapViewRegionDidChangeFromUserInteraction()
if (mapChangedFromUserInteraction) {
self.latitudineDelta = Float(mapView.region.span.latitudeDelta)
}
}
func mapViewRegionDidChangeFromUserInteraction() -> Bool {
let view = self.mapView.subviews[0]
if let gestureRecognizers = view.gestureRecognizers {
for recognizer in gestureRecognizers {
if(recognizer.state == UIGestureRecognizerState.ended)
{
return true
}
}
}
return false
}
fileprivate func centerMapOnLocation()
{
let coordinate = CLLocationCoordinate2D(latitude: self.latitude!,longitude: self.longitude!)
if (mapChangedFromUserInteraction == false)
{
let distance: CLLocationDistance = CLLocationDistance(Int(self.latitudineDelta * 111000))
self.camera = MKMapCamera(lookingAtCenter: coordinate,
fromDistance: distance,
pitch: pitch,
heading: self.heading)
self.mapView.setCamera(self.camera!, animated: isAnimated)
}
}