【问题标题】:Zoom MKMapView to fit polyline points缩放 MKMapView 以适合折线点
【发布时间】:2012-11-14 04:39:36
【问题描述】:

我有一个数组 allCollections,其中包含用户通过我的 iOS 应用程序记录的以编程方式创建的 CLLocations 数组。 allCollections 中的每个子数组都保存了一次行程中的所有位置点。

我从 allCollections 的数组中的 CLLocations 中绘制 MKPolylines 来表示 MKMapView 上的这些行程。我的问题是:将折线添加到地图后,我将如何以编程方式缩放和居中地图以显示所有折线?

【问题讨论】:

    标签: iphone objective-c ios mkmapview cllocation


    【解决方案1】:

    我有另一个解决这个问题的方法

    private func mapRegion() -> MKCoordinateRegion? {
    
    
        let latitudes = self.coordinates.map { location -> Double in
    
            return location.latitude
        }
    
        let longitudes = self.coordinates.map { location -> Double in
    
            return location.longitude
        }
    
        let maxLat = latitudes.max()!
        let minLat = latitudes.min()!
        let maxLong = longitudes.max()!
        let minLong = longitudes.min()!
    
        let center = CLLocationCoordinate2D(latitude: (minLat + maxLat) / 2,
                                            longitude: (minLong + maxLong) / 2)
        let span = MKCoordinateSpan(latitudeDelta: (maxLat - minLat) * 1.3,
                                    longitudeDelta: (maxLong - minLong) * 1.3)
        return MKCoordinateRegion(center: center, span: span)
    }
    

    其中坐标是CLLocationCoordinate2D的数组

    希望对大家有帮助

    【讨论】:

      【解决方案2】:

      Swift 4 /fundtimer 答案的略微修改版本。

       func setVisibleMapArea(polyline: MKPolyline, edgeInsets: UIEdgeInsets, animated: Bool = false) {
          mapView.setVisibleMapRect(polyline.boundingMapRect, edgePadding: edgeInsets, animated: animated)
      }
      

      使用路线的折线调用上述方法并保留默认的不动画,并在周围添加 10 的小边缘插入:

      setVisibleMapArea(polyline: route.polyline, edgeInsets: UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0))
      

      【讨论】:

      • 一个简单的解决方案 - 不错!
      【解决方案3】:

      @Fundtimer 指出了正确的方法,唯一的问题是 Padding 需要根据视觉需求进行调整,这样它将支持所有其他叠加层,以下是所有叠加层的通用解决方案。

      -(void)zoomIntoExistingMapObjectForAnnot:(CustomMapAnnotation *)annot
      {
         id overlay = annot.shape;//I have overlay property in custom annotation class.
         [_mapView setVisibleMapRect:[overlay boundingMapRect] edgePadding:UIEdgeInsetsMake(150.0, 150.0, 150.0, 150.0) animated:YES];
      }
      

      【讨论】:

        【解决方案4】:

        Swift 3版garafajon优秀代码

            if let first = self.mapView.overlays.first {
                let rect = self.mapView.overlays.reduce(first.boundingMapRect, {MKMapRectUnion($0, $1.boundingMapRect)})
                self.mapView.setVisibleMapRect(rect, edgePadding: UIEdgeInsets(top: 50.0, left: 50.0, bottom: 50.0, right: 50.0), animated: true)
            }
        

        【讨论】:

          【解决方案5】:

          迅速:

          if let first = mapView.overlays.first {
              let rect = mapView.overlays.reduce(first.boundingMapRect, combine: {MKMapRectUnion($0, $1.boundingMapRect)})
              mapView.setVisibleMapRect(rect, edgePadding: UIEdgeInsets(top: 50.0, left: 50.0, bottom: 50.0, right: 50.0), animated: true)
          }
          

          这将缩放/平移以适应所有覆盖并带有一点缓冲区

          【讨论】:

            【解决方案6】:
            -(void)zoomToPolyLine: (MKMapView*)map polyline: (MKPolyline*)polyline animated: (BOOL)animated
            {
                [map setVisibleMapRect:[polyline boundingMapRect] edgePadding:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0) animated:animated];
            }
            

            【讨论】:

            • @BradKoch 此解决方案使用boundingMapRect 定义一个包含折线覆盖的矩形。然后它使用setVisibleMapRect 将地图缩放区域设置为该矩形,同时考虑到edgePadding 的可调整填充值。我发现这个解决方案是最有帮助的,因为它用几行代码有效地解决了问题,并且还允许通过插图更改地图视图区域。
            • 这对我有用。但显然这会将缩放系数设置为 fill 屏幕,而不是 fit。你能想出一个解决方案吗
            • 加一个用于插入的解决方案。
            • 好的解决方案+1。唯一的问题是 Padding 需要根据视觉需求进行调整,这样它将支持所有其他叠加层。
            【解决方案7】:
            -(void)zoomToPolyLine: (MKMapView*)map polyLine: (MKPolyline*)polyLine 
            animated (BOOL)animated
            {
            MKPolygon* polygon = 
                [MKPolygon polygonWithPoints:polyLine.points count:polyLine.pointCount];
            
            [map setRegion:MKCoordinateRegionForMapRect([polygon boundingMapRect]) 
                 animated:animated];
            }
            

            【讨论】:

            • 不需要创建 MKPolygon,因为 MKPolyline 已经实现了 boundingMapRect
            • 你是如何将栅栏分配给 CLLocation 的?意味着在 goefencing 中如何分配 CLregion 而不是圆形区域???
            【解决方案8】:

            您可以遍历所有CLLocations 记录max/min 坐标并使用它来设置视图矩形,就像他们在这个问题iOS MKMapView zoom to show all markers 上所做的那样。

            或者您可以浏览每个叠加层并获取它们的boundingMapRect,然后使用MKMapRectUnion (http://developer.apple.com/library/ios/documentation/MapKit/Reference/MapKitFunctionsReference/Reference/reference.html#//apple_ref/c/func/MKMapRectUnion) 将它们全部组合起来,直到您有一个覆盖它们的MKMapRect 并使用它来设置视图.

            [mapView setVisibleMapRect:zoomRect animated:YES]
            

            这个问题显示了一个简单的循环,按照我的建议将联合中的 maprects 组合起来:MKMapRect zooms too much

            【讨论】:

            猜你喜欢
            • 2015-06-22
            • 1970-01-01
            • 1970-01-01
            • 2011-06-08
            • 1970-01-01
            • 2015-07-08
            • 1970-01-01
            • 2020-05-04
            相关资源
            最近更新 更多