【问题标题】:Clustering MGLAnnotations Images on iOS mapbox using MGLPointCollectionFeature使用 MGLPointCollectionFeature 在 iOS mapbox 上对 MGLAnnotations 图像进行聚类
【发布时间】:2020-07-21 00:31:59
【问题描述】:

使用最新的 Mapbox iOS SDK。具有不同图像的自定义 MGLAnnotations 都正确显示。我正在设置这样的注释:

self.mapView.addAnnotations(annotations)
self.setupClustering(for:annotations)

尝试使用 MGLPointCollectionFeature 而不是 GeojSON 文件来实现 mapbox 提供的example。 对大量进行硬编码以确保发生聚类。图标的实际宽度为 29。

编辑:没有聚类,没有出现带数字的圆圈

let clusterSource = MGLShapeSource(identifier: "clusterSource", shape: nil, options: [.clustered: true, .clusterRadius: 100])
func setupClustering(for annotations:[MGLAnnotation]) {
  let coordinates = annotations.map { $0.coordinate }
  let allAnnotationsFeature = MGLPointCollectionFeature(coordinates: coordinates, count: UInt(coordinates.count))
    self.clusterSource.shape = allAnnotationsFeature
}

func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
    self.mapView.style?.addSource(self.clusterSource)
    // Color clustered features based on clustered point counts.
    let stops = [
        2: UIColor.lightGray,
        5: UIColor.orange,
        10: UIColor.red
    ]
    
    // Show clustered features as circles. The `point_count` attribute is built into
    // clustering-enabled source features.
    let circlesLayer = MGLCircleStyleLayer(identifier: "clusteredItems", source: self.clusterSource)
    circlesLayer.circleRadius = NSExpression(forConstantValue: NSNumber(value: Double(100) / 2))
    circlesLayer.circleOpacity = NSExpression(forConstantValue: 0.75)
    circlesLayer.circleStrokeColor = NSExpression(forConstantValue: UIColor.white.withAlphaComponent(0.75))
    circlesLayer.circleStrokeWidth = NSExpression(forConstantValue: 2)
    circlesLayer.circleColor = NSExpression(format: "mgl_step:from:stops:(point_count, %@, %@)", UIColor.lightGray, stops)
    circlesLayer.predicate = NSPredicate(format: "cluster == YES")
    self.mapView.style?.addLayer(circlesLayer)
    
    // Label cluster circles with a layer of text indicating feature count. The value for
    // `point_count` is an integer. In order to use that value for the
    // `MGLSymbolStyleLayer.text` property, cast it as a string.
    let numbersLayer = MGLSymbolStyleLayer(identifier: "clusterNumbers", source: self.clusterSource)
    numbersLayer.textColor = NSExpression(forConstantValue: UIColor.white)
    numbersLayer.textFontSize = NSExpression(forConstantValue: NSNumber(value: Double(100) / 2))
    numbersLayer.iconAllowsOverlap = NSExpression(forConstantValue: true)
    numbersLayer.text = NSExpression(format: "CAST(point_count, 'NSString')")
    numbersLayer.predicate = NSPredicate(format: "cluster == YES")
    self.mapView.style?.addLayer(numbersLayer)
}

【问题讨论】:

  • 你还没有准确地解释你的问题是什么。
  • 很抱歉你是对的,我认为这是不言而喻的。没有得到聚类,仍然看到相互重叠的 MGLAnnotation 图像,或者它可能只是 1,但期望一个带有数字的圆圈作为聚类。

标签: ios mapbox


【解决方案1】:

您似乎正在使用形状而不是特征来初始化 MGLShapeSource (https://docs.mapbox.com/ios/api/maps/6.0.0/Classes/MGLShapeSource.html#/c:objc(cs)MGLShapeSource(im)initWithIdentifier:features:options:)。这会阻止您的点进行聚类,因为传入此初始化程序的任何 MGLFeature 实例都被视为普通形状,导致在评估谓词或配置某些布局或绘制属性时,MGLVectorStyleLayer 无法访问任何属性。

此外,对于聚类,您可能希望使用here 中提到的点形状。目前,您使用的是MGLPointCollectionFeatures 而不是MGLPointFeatures,这意味着您拥有一个包含五个点和一个特征的点集合特征,而不是具有 5 个特征的 5 个特征,每个特征都有自己的属性相同的属性。

如果您改为创建 MGLPointFeatures 数组,则可以在形状源初始值设定项的 features 参数中使用它们。

我在下面提供了一个示例:

var unclusteredFeatures = [MGLPointFeature]()

for coordinate in coordinates {
   let point = MGLPointFeature()
   point.coordinate = coordinate
   unclusteredFeatures.append(point)
}

let source = MGLShapeSource(identifier: "Features",
                                   features: unclusteredFeatures,
                                   options: [.clustered: true, .clusterRadius: 20])
style.addSource(source)

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 1970-01-01
    • 2013-12-10
    • 2018-02-02
    • 1970-01-01
    • 2017-08-28
    • 2015-05-09
    • 1970-01-01
    • 2020-02-16
    相关资源
    最近更新 更多