有没有人有Mapbox聚类与MGLAnnotations的工作示例,而不是geoJSON源文件?
给定一个样式。
let style: MGLStyle
1) 构建或获取你的 [MGLPointFeature] 对象
2) 构建一个 MGLShapeSource 对象
let source = MGLShapeSource(identifier: "YOUR_IDENTIFIER_A", features: YOUR_ MGLPointFeature_ARRAY, options: [.clustered: true, .clusterRadius: YOUR_WIDTH])
style.addSource(source)
3) 构建一个样式,用于未聚集的标记
let markerLayer = MGLSymbolStyleLayer(identifier: "YOUR_IDENTIFIER_B", source: source)
markerLayer.iconImageName = NSExpression(forConstantValue: "MARKER_IDENTIFIER")
markerLayer.predicate = NSPredicate(format: "cluster != YES")
style.setImage(UIImage(named: "MARKER_IMAGE")!, forName: "MARKER_IDENTIFIER")
4) 为集群建立一个样式
let clusterLayer = MGLSymbolStyleLayer(identifier: "YOUR_IDENTIFIER_C", source: source)
clusterLayer.textColor = NSExpression(forConstantValue: UIColor.white)
clusterLayer.textFontSize = NSExpression(forConstantValue: NSNumber(value: Double(YOUR_WIDTH) / 2.5))
clusterLayer.iconAllowsOverlap = NSExpression(forConstantValue: true)
clusterLayer.textOffset = NSExpression(forConstantValue: CGVector(dx: 0, dy: -0.2))
clusterLayer.predicate = NSPredicate(format: "cluster == YES")
style.setImage(UIImage(named: "CLUSTER_IMAGE")!, forName: "CLUSTER_IDENTIFIER")
5) 您可以使用“Stops”功能。这将允许您根据聚类计数更改集群图像。
let stops = [
10: NSExpression(forConstantValue: "CLUSTER_IMAGE"),
50: NSExpression(forConstantValue: "ANOTHER_CLUSTER_IMAGE")
]
6) 使用表达式根据定义的阈值设置每个集群的图像,并在相应的图像上显示点计数
let defaultShape = NSExpression(forConstantValue: "CLUSTER_IDENTIFIER")
clusterLayer.iconImageName = NSExpression(format: "mgl_step:from:stops:(point_count, %@, %@)", defaultShape, stops)
clusterLayer.text = NSExpression(format: "CAST(point_count, 'NSString')")
7) 给样式添加层级
style.addLayer(markerLayer)
style.addLayer(clusterLayer)