2016-09-28 7 views
1

基本的に2つの注釈を追加するマップがあり、それらは期待通りに表示されます。MapViewですべての注釈を表示し、できるだけズームインする

その後 - 私は、次のコードを使用して:

let annotations = [start, end] 
mapView?.showAnnotations(annotations, animated: false) 

をそして、これは2つの注釈と私の地図を表示するためにズームしますが、マップがではるかにズームインし、より詳細なマップビューを表示することができます。

This is what I get todayおよびthis is the expected resultが欲しい。

ご覧のとおり、地図をさらに拡大表示することができます。

これを実現する方法はありますか?

答えて

1

使用この:

extension MKMapView { 
    /// when we call this function, we have already added the annotations to the map, and just want all of them to be displayed. 
    func fitAll() { 
     var zoomRect   = MKMapRectNull; 
     for annotation in annotations { 
      let annotationPoint = MKMapPointForCoordinate(annotation.coordinate) 
      let pointRect  = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.01, 0.01); 
      zoomRect   = MKMapRectUnion(zoomRect, pointRect); 
     } 
     setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsetsMake(100, 100, 100, 100), animated: true) 
    } 

    /// we call this function and give it the annotations we want added to the map. we display the annotations if necessary 
    func fitAll(in annotations: [MKAnnotation], andShow show: Bool) { 
     var zoomRect:MKMapRect = MKMapRectNull 

     for annotation in annotations { 
      let aPoint   = MKMapPointForCoordinate(annotation.coordinate) 
      let rect   = MKMapRectMake(aPoint.x, aPoint.y, 0.1, 0.1) 

      if MKMapRectIsNull(zoomRect) { 
       zoomRect = rect 
      } else { 
       zoomRect = MKMapRectUnion(zoomRect, rect) 
      } 
     } 
     if(show) { 
      addAnnotations(annotations) 
     } 
     setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsets(top: 100, left: 100, bottom: 100, right: 100), animated: true) 
    } 

} 

が、その後、配列内のマップに追加のいずれかmapとして、そしてあなたの注釈とともにたとえば、あなたのMapViewのための出口を作成した後のような上記のメソッドにアクセス、annotationsと呼ばれますそう:

map.fitAll() 

OR

map.fitAll(in:annotations, true) 

それぞれ。スウィフトは

あなたが前に、かどうかのマップに注釈を追加したかどうかに応じて、最後の文の真または偽使用...

1

self.mapView.showAnnotations(self.mapView.annotations, animated: true) 

これは私がショーに使用するものですすべての注釈。あなたがしているのとほとんど同じです。マップはマップの可視または使用可能なセクションに合わせてこれをズームします。私のアプリではズームは良好で、ピンのどれもがエッジに触れないようにいくつかのパディングを追加します。

地図が完成したら、すぐにsetVisibleMapRect:edgePadding:animated:新しいvisibleRectを負の埋め込みで使用することができます。

関連する問題