如何根据缩放级别隐藏和显示MapBox
批注视图
->我正在使用MkMapView
MkAnnotationView隐藏并正常显示
->但是使用MapBox
隐藏并显示不起作用
->听到我正在为mapKit
工作编写此代码
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
}
}
}
}
请帮助我如何基于MapBox缩放级别隐藏和显示MapBoxAnnotationView 谢谢@mannaiah
答案 0 :(得分: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
委托方法中手动调用它。