【问题标题】:How can I expand a MKMapRect by a fixed percentage?如何按固定百分比扩展 MKMapRect?
【发布时间】:2012-12-23 23:50:20
【问题描述】:

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

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

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

【问题讨论】:

标签: objective-c mapkit


【解决方案1】:

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

}

【讨论】:

  • 很好的答案。用户,我为该调用提供了最新的语法,请随时编辑等。干杯。
【解决方案2】:

如何将visibleMapRect 转换为带有rectForMapRect:CGRect,使用CGRectInset 获得一个新的CGRect,然后将其转换回带有mapRectForRect:MKMapRect

【讨论】:

  • 这实际上是不正确的,2017 年。谢天谢地,Apple 已经完成了所有工作。 mapRectThatFits#edgePadding
【解决方案3】:

Xcode 10+、Swift 4.2

的简单而干净的解决方案

只需像这样为地图的边距设置 edge insets

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

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

【讨论】:

    【解决方案4】:

    针对 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)
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-09
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 2019-09-02
      • 1970-01-01
      • 2010-11-13
      • 2012-10-31
      相关资源
      最近更新 更多