12

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

如果地图矩形的值是可识别的单位,但原点 x 和 y 值大约为 4.29445e+07,并且宽度/高度为 2500-3000,这可能会更容易。

我从编写一个类别到手动执行此操作大约需要 10 秒,但我想确保我没有首先遗漏一些东西。有没有更简单的方法来扩展 MKMapRect?

4

4 回答 4

29

在 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];

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)

}
于 2013-10-10T22:26:46.390 回答
5

将 a转换visibleMapRectCGRectwith rectForMapRect:,获取一个新CGRect的 withCGRectInset然后将其转换回 a MKMapRectwithmapRectForRect:怎么样?

于 2013-01-30T21:05:50.643 回答
1

适用于Xcode 10+、Swift 4.2的简单而干净的解决方案

只需像这样为地图的边距设置边缘插图:

self.mapView.layoutMargins = UIEdgeInsets(top: 8, right: 8, bottom: 8, left: 8)
self.mapView.showAnnotations(map.annotations, animated: true)

请让我们知道它是否适合您。

于 2019-04-25T21:48:22.790 回答
0

改进和更正了 user2285781对 Swift 4 的回答:

    // 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)
    }
于 2017-09-07T11:55:30.320 回答