【问题标题】:Zoom MKMapView to fit polyline points in Swift缩放 MKMapView 以适应 Swift 中的折线点
【发布时间】:2015-06-22 23:41:05
【问题描述】:

我一直在寻找一种解决方案来放大 MapView 以适应 Swift 中 MKPolyline 的边界。我已经能够找到 Objective-C here on SO 的示例代码,但我一点也不熟悉 Objective-C 或如何将其转换为 Swift。

有人在 Swift 中有这样的例子吗?谢谢。

-(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];
}

【问题讨论】:

  • 查看this answer 中的最后两行代码(不需要循环或任何东西——只需let edgeInset = ... 之后的代码)。将 allAnnMapRect 替换为 polyline.boundingMapRect。
  • 安娜,这很好。感谢您的帮助。

标签: objective-c swift mkmapview mkpolyline


【解决方案1】:

在计算完方向后,在下面的代码行中写下这段代码。下面的代码对我有用。

//this code is used to fit two points polyline in mapview
  let newDistance = CLLocation(latitude: pickupCoordinate.latitude, longitude: pickupCoordinate.longitude).distance(from: CLLocation(latitude: destinationCoordinate.latitude, longitude: destinationCoordinate.longitude))
  let region = MKCoordinateRegion(center: pickupCoordinate, latitudinalMeters: 2 * newDistance, longitudinalMeters: 2 * newDistance)
  let adjustRegion = self.mapView.regionThatFits(region)
  self.mapView.setRegion(adjustRegion, animated:true)

截图如下。

希望对某人有所帮助。

【讨论】:

    【解决方案2】:
    func zoomToPolyLine(map : MKMapView, polyLine : MKPolyline, animated : Bool)
    {
     map.setRegion(MKCoordinateRegionForMapRect(polyLine.boundingMapRect), animated: animated)
     }
    

    【讨论】:

      【解决方案3】:

      此代码将区域设置为显示整条折线,以及每边 12.5% 的填充。

              var regionRect = myPolyline.boundingMapRect
      
      
              var wPadding = regionRect.size.width * 0.25
              var hPadding = regionRect.size.height * 0.25
      
              //Add padding to the region
              regionRect.size.width += wPadding
              regionRect.size.height += hPadding
      
              //Center the region on the line
              regionRect.origin.x -= wPadding / 2
              regionRect.origin.y -= hPadding / 2
      
              myMapView.setRegion(MKCoordinateRegionForMapRect(regionRect), animated: true)
      

      如果你不想要填充就这样做

              var regionRect = myPolyline.boundingMapRect
              myMapView.setRegion(MKCoordinateRegionForMapRect(regionRect), animated: true)
      

      【讨论】:

        猜你喜欢
        • 2012-11-14
        • 1970-01-01
        • 2011-06-08
        • 1970-01-01
        • 2015-07-08
        • 2018-12-21
        • 1970-01-01
        • 2015-06-26
        相关资源
        最近更新 更多