如何简单地检测缩放级别的变化?可能吗?

当缩放级别不够时,我只需要隐藏我的注释视图即可。
regionDidChange:animated:不适用于我。还有其他方法吗?

我需要在这里隐藏标签:

ios - MapBox-检测zoomLevel的变化-LMLPHP

并在此处显示:

ios - MapBox-检测zoomLevel的变化-LMLPHP

这是我目前对标签所做的操作:

class CardAnnotation: MGLPointAnnotation {

    var card: Card

    init(card: Card) {
        self.card = card
        super.init()

        let coordinates = card.border.map { $0.coordinate }
        let sumLatitudes = coordinates.map { $0.latitude }.reduce(0, +)
        let sumLongitudes = coordinates.map { $0.longitude }.reduce(0, +)
        let averageLatitude = sumLatitudes / Double(coordinates.count)
        let averageLongitude = sumLongitudes / Double(coordinates.count)

        coordinate = CLLocationCoordinate2D(latitude: averageLatitude, longitude: averageLongitude)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
var annotations = [CardAnnotation]()
mapView.addAnnotations(annotations)

最佳答案

MGLMapViewMGLMapViewDelegate.mapView(_:didFinishLoading:)这两个主要ways to add overlays中,运行时样式API更适合于文本标签,还适合根据缩放级别更改外观。在使用时,您也可以使用相同的API创建多边形。

首先为要着色的区域创建多边形要素:

var cards: [MGLPolygonFeature] = []
var coordinates: [CLLocationCoordinate2D] = […]
let card = MGLPolygonFeature(coordinates: &coordinates, count: UInt(coordinates.count))
card.attributes = ["address": 123]
// …
cards.append(card)

在地图完成加载后运行的任何方法(例如MGLSymbolStyleLayer)内,将包含以下特征的形状源添加到当前样式:
let cardSource = MGLShapeSource(identifier: "cards", features: cards, options: [:])
mapView.style?.addSource(cardSource)

放置好形状源后,创建一个样式层,以淡紫色填充的方式呈现多边形要素:
let fillLayer = MGLFillStyleLayer(identifier: "card-fills", source: cardSource)
fillLayer.fillColor = NSExpression(forConstantValue: #colorLiteral(red: 0.9098039216, green: 0.8235294118, blue: 0.9647058824, alpha: 1))
mapView.style?.addLayer(fillLayer)

然后创建另一个样式层,以在每个多边形要素的质心处呈现标签。 (MGLSymbolStyleLayer自动计算形心,考虑形状不规则的多边形。)
// Same source as the fillLayer.
let labelLayer = MGLSymbolStyleLayer(identifier: "card-labels", source: cardSource)
// Each feature’s address is an integer, but text has to be a string.
labelLayer.text = NSExpression(format: "CAST(address, 'NSString')")
// Smoothly interpolate from transparent at z16 to opaque at z17.
labelLayer.textOpacity = NSExpression(format: "mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)",
                                      [16: 0, 17: 1])
mapView.style?.addLayer(labelLayer)

定制这些样式层时,请特别注意textOpacity上的选项,这些选项控制附近的符号是否由于碰撞而自动隐藏。您可能会发现自动冲突检测使无需指定MGLShapeSource属性。

创建源时,可以传递给MGLShapeSourceOption.clustered初始化程序的选项之一是MGLPointFeature。但是,要使用该选项,您必须创建MGLPolygonFeature而不是MGLPolygonFeature。幸运的是,coordinate具有MGLSymbolStyleLayer属性,可让您无需手动计算即可找到质心:
var cardCentroids: [MGLPointFeature] = []
var coordinates: [CLLocationCoordinate2D] = […]
let card = MGLPolygonFeature(coordinates: &coordinates, count: UInt(coordinates.count))
let cardCentroid = MGLPointFeature()
cardCentroid.coordinate = card.coordinate
cardCentroid.attributes = ["address": 123]
cardCentroids.append(cardCentroid)
// …
let cardCentroidSource = MGLShapeSource(identifier: "card-centroids", features: cardCentroids, options: [.clustered: true])
mapView.style?.addSource(cardCentroidSource)

此群集源只能与MGLCircleStyleLayerMGLFillStyleLayer一起使用,不能与ojit_code一起使用。 This example详细显示了如何使用聚类点。

10-08 06:01