【问题标题】:MKMapView visible region paddingMKMapView 可见区域填充
【发布时间】:2015-02-08 09:14:03
【问题描述】:

德拉的同事们,

我很难用原生 MKMapView 实现可见区域。 我需要拥有与Google Map SDK Visible Region 完全相同的功能。

基本上这意味着在下面的代码底部有一种填充:

let span = MKCoordinateSpan(latitudeDelta: 0.8, longitudeDelta: 0.8)
let reg = MKCoordinateRegion(center: someMyCoordinate, span: span)
mapView.setRegion(reg, animated: true)

它需要是这样的:一旦我在 UIView 坐标中添加了一些填充,它应该移动地图中心并调整缩放以使坐标区域完全可见,考虑到填充。

可能我的描述有点乱,但是如果你看一下this video就会变得非常清楚。

提前谢谢你!

【问题讨论】:

  • 视图/文本不应覆盖您的 mapView,这是您的问题吗?
  • 是的。 UIView 应该。此视图的高度应用作填充。

标签: ios swift mkmapview google-maps-sdk-ios


【解决方案1】:

如果您打印可见区域,您会看到跨度将被填充,因为它需要适合视图。 中心坐标仍将位于视图的中心。 我不确定你想做什么,但我想它可以存档

setVisibleMapRect:edgePadding:animated:

您需要将该区域转换为 MKMapRect。见Convert MKCoordinateRegion to MKMapRect 关于如何做到这一点

【讨论】:

    【解决方案2】:

    你需要像下面这样设置一些逻辑坐标并设置区域,然后你可以设置与谷歌地图相同的缩放级别。

    MKCoordinateRegion 区域;

    CLLocationDegrees maxLat = -90;
    CLLocationDegrees maxLon = -180;
    CLLocationDegrees minLat = 90;
    CLLocationDegrees minLon = 180;
    

    浮动当前纬度;

    浮动当前经度;

    如果(当前纬度 > 最大纬度)

    maxLat = currentLatitude;
    

    如果(当前纬度

    minLat = currentLatitude;
    

    if(currentLongitude > maxLon)

    maxLon = currentLongitude;
    

    如果(当前经度

    minLon = currentLongitude;
    

    region.center.latitude = (maxLat + minLat) / 2;

    region.center.longitude = (maxLon + minLon) / 2;

    region.span.latitudeDelta = maxLat - minLat;

    region.span.longitudeDelta = maxLon - minLon;

    [myMapview setRegion:region animated:YES];

    [myMapview setCenterCoordinate:theCoordinate zoomLevel:13 animated:YES];

    【讨论】:

      【解决方案3】:

      感谢上面建议的方法,完整的解决方案如下。

      import Foundation
      import MapKit
      extension MKCoordinateRegion{
          var mapRect:MKMapRect {
              get{
                  let a = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
                         self.center.latitude + self.span.latitudeDelta / 2,
                         self.center.longitude - self.span.longitudeDelta / 2))
      
                  let b = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
                          self.center.latitude - self.span.latitudeDelta / 2,
                          self.center.longitude + self.span.longitudeDelta / 2))
      
                  return MKMapRectMake(min(a.x,b.x), min(a.y,b.y), abs(a.x-b.x), abs(a.y-b.y))
              }
          }
      }
      
      extension MKMapView {
          func setVisibleRegion(mapRegion: MKCoordinateRegion, edgePadding insets: UIEdgeInsets, animated animate: Bool) {
              self.setVisibleMapRect(mapRegion.mapRect, edgePadding: insets , animated: animate)
          }
      }
      

      现在您可以使用 setVisibleRegion 函数了。

      【讨论】:

        【解决方案4】:

        谷歌填充背后的原因是为了让谷歌按钮和版权不受阻碍,这样您就可以在地图边缘提供自己的用户界面。 (如谷歌地图视频所示)

        从 iOS 11 开始,Apple 解决了同样的问题,而不是填充,而是让您控制在哪里放置带有新类的控件。那些是

        MKScaleView
        MKCompassButton
        MKUserTrackingButton
        

        将它们放置在地图上的任何位置,甚至是地图之外。 它们与MKMapView 完全集成。

        Apple 忘记的唯一控件是左下角的合法链接(至少对于 ltr 语言)。所以这个链接总是妨碍我们的设计,至少在 iOS 11 中是这样。

        也许您真的想要填充,然后选择其他答案。 但是,如果您想控制放置 Apple 控件并且可以不支持 iOS 11 之前的 iOS 版本,那么请使用新类。

        【讨论】:

          【解决方案5】:

          如果您需要在 swift 中从 100 的底部开始填充,您可以简单地编写:

          mapView.layoutMargins = UIEdgeInsets(top: 10, right: 10, bottom: 100, left: 10)
          

          然后你可以检查这个以地图为中心的一些注释,如下所示:

          mapView.showAnnotations(mapView.annotations, animated: true)
          

          【讨论】:

          • 先生,您救了我的命。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多