如何根据缩放级别隐藏和显示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
您可以尝试将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
委托方法中手动调用它。