如何按固定百分比扩展MKMapRect?

Nic*_*ick 12 objective-c mapkit

我想得到一个结果MKMapRect,它在所有方向上比当前大10-20%visibleMapRect.如果这是一个CGRect,我会使用带有负x和y值的CGRectInset,为我提供一个反向插图(即一个更大的rect).不幸的是,MKMapInset不支持负插入值,所以它并不那么容易.

如果map rect的值是可识别单位但原点x和y值大约为4.29445e + 07,宽度/高度为2500-3000,则可能更容易.

我写一个类别手动做了大约10秒,但我想确保我没有先丢失一些东西.有没有更简单的方法来扩展MKMapRect?

小智 28

在iOS7,rectForMapRect:mapRectForRect:已被弃用,现在是部分MKOverlayRenderer类.我宁愿建议使用这些MapView mapRectThatFits: edgePadding:方法.这是一个示例代码:

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

2017年最新款Swift ......

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)

}
Run Code Online (Sandbox Code Playgroud)


Fáb*_*ira 5

怎么样转换visibleMapRectCGRectrectForMapRect:,得到一个新的CGRectCGRectInset,然后将其转换回MKMapRectmapRectForRect: