7

マップビューに表示する 2 つの注釈がありますが、ユーザーがズームアウトしなくてもすべての注釈を画面に表示するように maprect を設定することはできません。

試してみましshowAnnotationsたが、運がありませんでした。Swift と Xcode 6.1.1 でこれを実行できた人はいますか?

これが私のコードです:

class ViewController: UIViewController, MKMapViewDelegate {


    @IBOutlet var map: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        var mapView = map
        // 1
        let point1 = CLLocationCoordinate2D(latitude: 38.915565, longitude: -77.093524)
        let point2 = CLLocationCoordinate2D(latitude: 38.890693, longitude: -76.933318)

        //2
        let annotation = MKPointAnnotation()
        annotation.setCoordinate(point1)
        annotation.title = "point1"
        map.addAnnotation(annotation)

        let annotation2 = MKPointAnnotation()
        annotation2.setCoordinate(point2)
        annotation2.title = "point2"
        map.addAnnotation(annotation2)

        //3
        // option1: set maprect to cover all annotations, doesn't work
        var points = [annotation, annotation2]
        var rect = MKMapRectNull
        for p in points {
            let k = MKMapPointForCoordinate(p.coordinate)
            rect = MKMapRectUnion(rect, MKMapRectMake(k.x, k.y, 0.1, 0.1))
            println("result: x = \(rect.origin.x) y = \(rect.origin.y)")
        }

        map.setVisibleMapRect(rect, animated: true)

        // option 2: using showAnnotations, doesn't work
        //map.showAnnotations(points, animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


     }

これは私が現在得たものです: ここに画像の説明を入力

これは私が期待したものです: ここに画像の説明を入力

ご協力いただきありがとうございます。

4

4 に答える 4

20

アノテーションのピンが画面の可視領域に表示されていなかった理由がようやくわかりました。MapKit フレームワークは、以前のバージョンとは少し異なる動作をしていると思います。autolayout を使用して、すべてのデバイス (iPhone、iPad) の画面全体にマップを拡大できるようにしているため、マップの setVisibleMapRect または mapView.showAnnotations は、ビュー コントローラーの viewDidLoad ではなく、mapViewDidFinishLoadingMap で呼び出す必要があります。

例えば:

 func mapViewDidFinishLoadingMap(_ mapView: MKMapView) {
     // this is where visible maprect should be set
     mapView.showAnnotations(mapView.annotations, animated: true)  
 }
于 2015-01-27T16:03:20.180 に答える