隐藏和显示MapBox注释图像视图基于缩放级别

问题描述 投票:0回答:1

MapBox MglMapView

如何根据缩放级别隐藏和显示MapBox注释视图

- >我正在使用MkMapView MkAnnotationView隐藏并显示工作正常

- >但是使用MapBox隐藏并显示不工作 - >听到我写的这个代码为mapKit Working

  func mapView(_ mapView: MGLMapView, regionDidChangeAnimated animated: Bool) {


  let mglVisiableAnnArray = self.mglMap.visibleAnnotations

                        if mglVisiableAnnArray != nil {

                            for annotation in mglVisiableAnnArray!
                            {
                                if self.mglMap.zoomLevel < 12.5
                                {
                                    self.mglMap.view(for:annotation)?.isHidden = true

                                }else{
                            self.mglMap.view(for:annotation)?.isHidden = false

                                }
                            }
                        }
}

请帮助我如何隐藏和显示MapBoxAnnotationView基于MapBox缩放级别谢谢@mannaiah

ios swift mapbox
1个回答
0
投票

您可以尝试将MGLPointAnnotations添加到Shape源,这会将所有注释添加到地图中。

var myAnnotations = [your annotations...]

private func configureSource(style: MGLStyle) -> MGLShapeSource {
    var annotations = [MyCustomAnnotation]()
    for annotation in self.myAnnotations {
      if let myAnnotation = annotation as? MyCustomAnnotation {
        annotations.append(myAnnotation)
        let imageName = myAnnotation.annotationImageName
        if let image = UIImage(named: imageName) {
          style.setImage(image, forName: imageName)
        }
      }
    }

    return MGLShapeSource(identifier: "myAnnotations", features: annotations, options: nil)
  }
func configureSymbolLayer(source: MGLShapeSource) -> MGLSymbolStyleLayer {
  let symbols = MGLSymbolStyleLayer(identifier: "identifier", source: source)
  symbols.iconImageName = NSExpression(forKeyPath: "annotationImageName")

  if self.mapBoxView.zoomLevel < zoomThreshold {
    symbols.iconOpacity = NSExpression(forConstantValue: "0.0")
  } else {
    symbols.iconOpacity = NSExpression(forConstantValue: "1.0")
  }

  return symbols
}

然后,您需要在didFinishLoading style委托方法中添加源和样式。

func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
  guard let style = mapView.style else { return }

  let source = configureSource(style: style)
  style.addSource(source)
  style.addLayer(configureSymbolLayer(source: source))
}

当区域被更改时,默认情况下应调用mapView的重载样式函数,但如果不是,则可能需要在regionDidChanged委托方法中手动调用它。

© www.soinside.com 2019 - 2024. All rights reserved.