【问题标题】:MKMapView's scale is not shownMKMapView 的比例不显示
【发布时间】:2017-11-13 20:05:40
【问题描述】:

我正在做一个 iOS 应用程序。在 Xcode 9.1 中,我通过

创建了一个 MKMapView
let mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height))
mapView.isUserInteractionEnabled = false
mapView.mapType = .satellite
mapView.showsCompass = false
mapView.showsScale = true
view.addSubview(mapView)

但是当我在模拟器中运行它时,未显示比例,并且我在日志中收到三条消息:

无法从边缘 9 插入指南针

无法从边缘 9 插入比例

无法从角落 4 插入法律归属

指南针没有显示(如预期的那样),但如果我将mapView.showsCompass 更改为trueeither,它也不会显示。但是,会显示 Legal 链接。我在这里想念什么?我猜这与 iOS 11 引入的新安全区域有关,但我看不出这对于我想要覆盖整个屏幕的视图有多重要。

【问题讨论】:

  • 在 iOS 11 中,指南针仅在地图远离北方旋转并且缩放时显示比例时才会显示。我推荐关于 MapKit 新功能的 WWDC 会议。我认为您可以忽略这些消息。
  • WWDC 会议解释了如何添加指南针按钮以始终显示指南针
  • 我将观看 WWDC 会议,但问题是关于展示规模。代码将指南针设置为不显示,所以这不是问题。
  • 比例尺默认只在缩放时显示。该视频还讨论了您可以添加的新比例视图

标签: ios swift mkmapview


【解决方案1】:

在 iOS 10 或更低版本中

正如@Paulw11 所说,默认情况下仅在缩放时显示比例。

在 iOS 11 中

您可以使用scaleVisibilityhttps://developer.apple.com/documentation/mapkit/mkscaleview/2890254-scalevisibility

let scale = MKScaleView(mapView: mapView)
scale.scaleVisibility = .visible // always visible
view.addSubview(scale)

【讨论】:

    【解决方案2】:

    今天的体重秤也有同样的问题。我希望这个比例一直可见。花了我几个小时来解决它。所以我在这里添加代码,以防万一有人遇到同样的问题。

    得到一些提示:

    来自这个帖子:Use Safe Area Layout programmatically

    还有这个网站:Pain Free Constraints with Layout Anchors

    编码愉快...

    哈迪

    // "self.MapOnScreen" refers to the map currently displayed
    
    // check if we have to deal with the scale
    if #available(iOS 11.0, *) {
    
        // as we will change the UI, ensure it's on main thread
        DispatchQueue.main.async(execute: {
    
            // switch OFF the standard scale (otherwise both will be visible when zoom in/out)
            self.MapOnScreen.showsScale = false
    
            // build the view
            let scale = MKScaleView(mapView: self.MapOnScreen)
    
            // we want to use autolayout
            scale.translatesAutoresizingMaskIntoConstraints = false
    
            // scale should be visible all the time
            scale.scaleVisibility = .visible // always visible
    
            // add it to the map
            self.MapOnScreen.addSubview(scale)
    
            // get the current safe area of the map
            let guide = self.MapOnScreen.safeAreaLayoutGuide
    
            // Activate this array of constraints, which at the time removes leftovers if any
            NSLayoutConstraint.activate(
                [
                    // LEFT (I do not want a change if right-to-left language) margin with an offset to safe area
                    // alternative would be ".leadingAnchor", which switches to the right margin, if right-to-left language is used        
                    scale.leftAnchor.constraint(equalTo: guide.leftAnchor, constant: 16.0),
    
                    // right edge will be the middle of the map
                    scale.rightAnchor.constraint(equalTo: guide.centerXAnchor),
    
                    // top margin is the top safe area
                    scale.topAnchor.constraint(equalTo: guide.topAnchor),
    
                    // view will be 20 points high
                    scale.heightAnchor.constraint(equalToConstant: 20.0)
                ]
            )
        })
    }
    

    【讨论】:

      【解决方案3】:

      Objective c 等价物:-

      if (@available(iOS 11.0, *)) {
              // switch OFF the standard scale (otherwise both will be visible when zoom in/out)
          self.map.showsScale = false;
      
              // build the view
      
          MKScaleView* scale = [MKScaleView scaleViewWithMapView:self.map];
      
              // we want to use autolayout
          scale.translatesAutoresizingMaskIntoConstraints = false;
      
              // scale should be visible all the time
          scale.scaleVisibility = MKFeatureVisibilityVisible;// always visible
          
          // add it to the map
          [self.view addSubview:scale];
         
              // get the current safe area of the map
          UILayoutGuide * guide = self.view.safeAreaLayoutGuide;
      
              // Activate this array of constraints, which at the time removes leftovers if any
              [NSLayoutConstraint activateConstraints:
                  @[
                      // LEFT (I do not want a change if right-to-left language) margin with an offset to safe area
                      // alternative would be ".leadingAnchor", which switches to the right margin, if right-to-left language is used
                     //[scale.leftAnchor constraintEqualToAnchor: guide.centerXAnchor constant: -(scale.frame.size.width/2.0)],
                
                      // right edge will be the middle of the map
                      [scale.rightAnchor constraintEqualToAnchor: guide.centerXAnchor constant: (scale.frame.size.width/2.0)],
      
                      // top margin is the top safe area
                      [scale.bottomAnchor constraintEqualToAnchor: guide.bottomAnchor constant:-self.toolBar.frame.size.height],
      
                      // view will be 20 points high
                      [scale.heightAnchor constraintEqualToConstant: 50.0]
                  ]
              ];
          
          [self.view bringSubviewToFront:scale];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        相关资源
        最近更新 更多