【发布时间】: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,但期望一个带有数字的圆圈作为聚类。