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

问题描述 投票:11回答:4

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

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

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

objective-c mapkit
4个回答
26
投票

在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)

}

5
投票

怎么样用visibleMapRectCGRect转换为rectForMapRect:,用CGRect获得一个新的CGRectInset然后用MKMapRect将其转换回mapRectForRect:


0
投票

为Swift 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)
    }

0
投票

适用于Xcode 10 +,Swift 4.2的简单清洁解决方案

只需设置地图边距的边缘插图,如下所示:

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

如果它适合您,请告诉我们。

© www.soinside.com 2019 - 2024. All rights reserved.