私のswift
アプリケーションでは、私はMapKit
を使用しており、いくつかの注釈をマップに追加しています。注釈ビューが画面の端に近づいている場合にのみ、その注釈ビューに地図を配置するにはどうすればよいですか?
現在、この質問のおかげで、Trying to get the span size in meters for an iOS MKCoordinateSpan私はユーザーがそれを押したときに注釈にマップを集中させる機能を作成することができました。これはそれのためのコードです:
let span = mapView.region.span
let centerView = mapView.region.center
let loc1 = CLLocation(latitude: centerView.latitude - span.latitudeDelta * 0.5, longitude: centerView.longitude)
let loc2 = CLLocation(latitude: centerView.latitude + span.latitudeDelta * 0.5, longitude: centerView.longitude)
let loc3 = CLLocation(latitude: centerView.latitude, longitude: centerView.longitude - span.longitudeDelta * 0.5)
let loc4 = CLLocation(latitude: centerView.latitude, longitude: centerView.longitude + span.longitudeDelta * 0.5)
let metersInLatitude = loc1.distance(from: loc2)
let metersInLongitude = loc3.distance(from: loc4)
let center:CLLocationCoordinate2D = (MyCustomAnnotation?.coordinate)!
let coordinateRegion = MKCoordinateRegionMakeWithDistance(center, metersInLatitude, metersInLongitude)
mapView.setRegion(coordinateRegion, animated: true)
、それが正常に動作します - 現在のズームレベルを維持しながら、ユーザーが任意のMyCustomAnnotation
をクリックするたびに、それはマップを中心に説明します。
このコードを少し変更したいので、ポイントが画面の4つのエッジの1つに近い場合にのみポイントを中心にします。注釈点が画面の中心に近いところにあるときは、地図を中心にすべきではありません。どうしたらいいですか? MyCustomAnnotation?.coordinate
とその4点で設定された領域の違いを(何とか)確認することを考えました。loc1
、loc2
、loc3
、loc4
ですが、どのように処理するかはわかりません。あなたはそれで私を助けることができますか?
なぜ乗算は0.3ですか? –
これはあなたが定義できる単なる要素です – Hannes