2016-05-18 9 views
0

マップ上に1つのピンの座標があります。私はそのピンにマップを集中させようとしますが、ピンの真ん中を画面の1/3の位置に保持したくありません。私は次の回避策でそれをやろうとしました:どのようにして、選択したピンがマップの中央になく、1/3になるようにマップビューを中央に配置できますか?

let coordinateRegion = MKCoordinateRegionMakeWithDistance(coordinatesOfThePin, 500, 500) 
mapView.setRegion(coordinateRegion, animated: true) 

let frame = mapView.frame 
let myPoint = CGPointMake(frame.midX, frame.midY/3) 
let myCoordinate = mapView.convertPoint(myPoint, toCoordinateFromView: mapView) 

let coordinateRegion2 = MKCoordinateRegionMakeWithDistance(myCoordinate, 500, 500) 

mapView.setRegion(coordinateRegion2, animated: true) 

ただし、マップの最初の位置によってランダムな位置が表示されます。あなたはポイントがスケールで現在のズームを使用する場合は

------------------- 
|     | 
|     | 
|  X  | <-- my pin in the 1/3 of the visible map area. 
|     | 
|     | 
|     | 
|     | 
|     | 
|     | 
|     | 
|     | 
------------------- 
+0

はhttp://stackoverflow.com/a/15421512/1271826 – Rob

+0

することができます参照してください。また、MKMapCameraを使用してください – jtbandes

答えて

3

が、座標上のちょうど中央:私はちょうどピン位置を入れて、以下のような結果を得ることができるように私のコードを書き換えで私を助けることができますそれが途中まで画面1 /第3回ですが、あなただけのcenterCoordinateを設定し、その後1/6 spanlatitudeDeltaそれを微調整できることが、このような:あなたが合うようcenterの調整を微調整

func updateMapForCoordinate(coordinate: CLLocationCoordinate2D) { 
    var center = coordinate; 
    center.latitude -= self.mapView.region.span.latitudeDelta/6.0; 
    mapView.setCenterCoordinate(center, animated: true); 
} 

を、しかし、うまくいけば、これは1つの簡単なアプローチを示す。明らかに、この小さなトリックは、マップにピッチがなく、回転しない場合にのみ機能します。


あなたが最初にズームしたい場合は、カメラを設定し、centerCoordinateをリセットすることができます。

func updateMapForCoordinate(coordinate: CLLocationCoordinate2D) { 
    let camera = MKMapCamera(lookingAtCenterCoordinate: coordinate, fromDistance: 1000, pitch: 0, heading: 0) 
    mapView.setCamera(camera, animated: false) 
    var center = coordinate; 
    center.latitude -= self.mapView.region.span.latitudeDelta/6.0; 
    mapView.setCenterCoordinate(center, animated: false); 
} 
+0

私はそれをして、それは素晴らしい、ありがとう!ここでもう一つのこと - 地図上でズームを設定する方法はありますか?このコードをボタンに貼り付けて、ユーザーが押したときに地図の中心をピンに合わせますが、ズームを少なくともストリートレベル(例:500)に調整することもできますか? – user3766930

+0

@ user3766930 - 次に、この改訂コードサンプルに示すように、カメラを最初に設定します。 – Rob

+0

@Robのコメントありがとうございますが、私は 'lookingAtCenterCoordinate'がiOS 9.0からのみ利用可能であることを知っています - おそらく古いアイオスで動作する可能性のある他の方法がありますか? – user3766930