使用 Mapbox 聚类注释
Clustering annotations with Mapbox
我正在尝试在 Mapbox 中为 iOS 实现聚类,实际上是使用 this example from Mapbox website
它工作正常,但我希望能够使用简单的 MGLAnnotations 将它们放置在地图上,并在它们距离太近时将它们聚集在一起。
我 read here MGLShapeSource 不仅接受外部 geoJSON,还接受其他来源,例如折线和注释。但是当我用注解数组喂它时,没有发生聚类,我只是从我的注解数组中看到一堆我的标记:
let source = MGLShapeSource(identifier: "clusteredParkings", shapes: annotationsArray, options: [.clustered: true, .clusterRadius: 20])
当我将源换回 geoJSON 时,集群的一切都再次正常工作。
顺便说一句,没有错误或警告。
我做错了什么?有人有使用 MGLAnnotations 而不是 geoJSON 源文件的 Mapbox 聚类的工作示例吗?
https://www.mapbox.com/ios-sdk/api/3.6.0/Classes/MGLShapeSource.html
不久前我对此进行了一些研究,但在 iOS 上似乎不可能。 Here is the suggestion on github that is still open. Here 是另一个问题,它在文档中未提及,但已被添加。
Does anyone have a working example of Mapbox clustering with MGLAnnotations rather than geoJSON source file?
给定样式
let style: MGLStyle
1) Build/Get 你的 [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) 您可以使用止损功能。这将使您可以根据聚类计数的变化来更改聚类图像。
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)
我正在尝试在 Mapbox 中为 iOS 实现聚类,实际上是使用 this example from Mapbox website
它工作正常,但我希望能够使用简单的 MGLAnnotations 将它们放置在地图上,并在它们距离太近时将它们聚集在一起。
我 read here MGLShapeSource 不仅接受外部 geoJSON,还接受其他来源,例如折线和注释。但是当我用注解数组喂它时,没有发生聚类,我只是从我的注解数组中看到一堆我的标记:
let source = MGLShapeSource(identifier: "clusteredParkings", shapes: annotationsArray, options: [.clustered: true, .clusterRadius: 20])
当我将源换回 geoJSON 时,集群的一切都再次正常工作。 顺便说一句,没有错误或警告。
我做错了什么?有人有使用 MGLAnnotations 而不是 geoJSON 源文件的 Mapbox 聚类的工作示例吗?
https://www.mapbox.com/ios-sdk/api/3.6.0/Classes/MGLShapeSource.html
不久前我对此进行了一些研究,但在 iOS 上似乎不可能。 Here is the suggestion on github that is still open. Here 是另一个问题,它在文档中未提及,但已被添加。
Does anyone have a working example of Mapbox clustering with MGLAnnotations rather than geoJSON source file?
给定样式
let style: MGLStyle
1) Build/Get 你的 [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) 您可以使用止损功能。这将使您可以根据聚类计数的变化来更改聚类图像。
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)