2013-01-09 7 views
8

現在のvisibleMapRectよりもすべての方向で10-20%大きい結果のMKMapRectを取得したいとします。これがCGRectの場合は、負のxとyの値を持つCGRectInsetを使用して、逆インセット(すなわち、より大きな矩形)を提供します。残念ながら、MKMapInsetは負のインセット値をサポートしていないので、それほど簡単ではありません。MKMapRectを一定の割合で拡張するにはどうすればよいですか?

マップ矩形の値が認識可能な単位であっても、原点のx値とy値が4.29445e + 07のオーダーで、幅/高さが2500-3000の場合、これは簡単です。

手動でこれを行うカテゴリを書くのは約10秒ですが、最初に何かを逃していないことを確認したかったのです。 MKMapRectを簡単に展開する方法はありますか?

+0

このヘルプ? :http://stackoverflow.com/questions/8465149/mkmaprect-zooms-too-much – Firo

+0

メモ、2017年のためにAppleは今**すべての仕事を終えました**、以下の回答を参照してください – Fattie

答えて

20

iOS7では、rectForMapRect:mapRectForRect:は廃止され、現在はMKOverlayRendererクラスの一部です。むしろMapViewmapRectThatFits: edgePadding:メソッドを使用することをお勧めしたいと思います。 2017年

MKMapRect visibleRect = self.mapView.visibleMapRect; 
UIEdgeInsets insets = UIEdgeInsetsMake(50, 50, 50, 50); 
MKMapRect biggerRect = [self.mapView mapRectThatFits:visibleRect edgePadding:insets]; 

最新スウィフト

func updateMap() { 

    mkMap.removeAnnotations(mkMap.annotations) 
    mkMap.addAnnotations(yourAnnotationsArray) 

    var union = MKMapRectNull 

    for p in yourAnnotationsArray { 
     // make a small, say, 50meter square for each 
     let pReg = MKCoordinateRegionMakeWithDistance(pa.coordinate, 50, 50) 
     // convert it to a MKMapRect 
     let r = mkMapRect(forMKCoordinateRegion: pReg) 

     // union all of those 
     union = MKMapRectUnion(union, r) 

     // probably want to turn on the "sign" for each 
     mkMap.selectAnnotation(pa, animated: false) 
    } 

    // expand the union, using the new #edgePadding call. T,L,B,R 
    let f = mkMap.mapRectThatFits(union, edgePadding: UIEdgeInsetsMake(70, 0, 10, 35)) 

    // NOTE you want the TOP padding much bigger than the BOTTOM padding 
    // because the pins/signs are actually very tall 

    mkMap.setVisibleMapRect(f, animated: false) 

} 
+0

素晴らしい答え。ユーザー、私はその呼び出しのための最新の構文を投げた、などの歓声を編集する自由を感じる。 – Fattie

5

CGRectInsetで新しいCGRectを取得し、その後mapRectForRect:MKMapRectに戻ってそれを変換し、rectForMapRect:CGRectvisibleMapRectの変換について?

+0

これは実際には今、良さに感謝し、すべての仕事を今行った。 ** mapRectThatFits#edgePadding ** – Fattie

0

洗練された...とスウィフト4用user2285781's answerを修正:ここではサンプルコードはあります全く

// reference: https://stackoverflow.com/a/15683034/347339 
    func MKMapRectForCoordinateRegion(region:MKCoordinateRegion) -> MKMapRect { 
     let topLeft = CLLocationCoordinate2D(latitude: region.center.latitude + (region.span.latitudeDelta/2), longitude: region.center.longitude - (region.span.longitudeDelta/2)) 
     let bottomRight = CLLocationCoordinate2D(latitude: region.center.latitude - (region.span.latitudeDelta/2), longitude: region.center.longitude + (region.span.longitudeDelta/2)) 

     let a = MKMapPointForCoordinate(topLeft) 
     let b = MKMapPointForCoordinate(bottomRight) 

     return MKMapRect(origin: MKMapPoint(x:min(a.x,b.x), y:min(a.y,b.y)), size: MKMapSize(width: abs(a.x-b.x), height: abs(a.y-b.y))) 
    } 

    // reference: https://stackoverflow.com/a/19307286/347339 
    // assuming coordinates that create a polyline as well as a destination annotation 
    func updateMap(coordinates: [CLLocationCoordinate2D], annotation: MKAnnotation) { 

     var union = MKMapRectNull 
     var coordinateArray = coordinates 
     coordinateArray.append(annotation.coordinate) 

     for coordinate in coordinateArray { 
      // make a small, say, 50meter square for each 
      let pReg = MKCoordinateRegionMakeWithDistance(coordinate, 50, 50) 
      // convert it to a MKMapRect 
      let r = MKMapRectForCoordinateRegion(region: pReg) 

      // union all of those 
      union = MKMapRectUnion(union, r) 
     } 

     // expand the union, using the new #edgePadding call. T,L,B,R 
     let f = mapView.mapRectThatFits(union, edgePadding: UIEdgeInsetsMake(70, 35, 10, 35)) 

     // NOTE you want the TOP padding much bigger than the BOTTOM padding 
     // because the pins/signs are actually very tall 

     mapView.setVisibleMapRect(f, animated: false) 
    } 
関連する問題