使用Mapbox对注释进行聚类

9
我正在尝试在Mapbox for iOS中实现聚类,字面上使用Mapbox网站上的此示例。它可以正常工作,但我想能够使用简单的MGLAnnotations放置在地图上并将它们聚合在一起,如果它们太接近。
我在这里读到,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

2个回答

6

我不久前对此进行了一些研究,似乎在iOS上不可能实现。这里是github上仍然开放的建议。这里是另一个问题,关于它没有在文档中提到,但后来已经添加了。


1
是的,我也发现了。谢谢你。看来我们只能等待了。 - kernelpanic
4
这在2019年有没有改变? - user10813210

3
有没有人有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)

你能解释一下这是如何工作的吗?解释一下代码,我们为什么要这样做等等,然后我会奖励你的赏金。 - user10813210
1
代码第二部分的来源解释:https://docs.mapbox.com/ios/maps/examples/clustering-with-images/ - Nat
我不认为这是大多数人正在寻找的解决方案。 - user10497264
这是回答以下问题的解决方案:“有没有人有使用MGLAnnotations而不是geoJSON源文件的Mapbox聚类的工作示例?”:也许您正在寻找其他内容。 - Sergio Andreotti
@SergioAndreotti,请将工作项目加载到GitHub上,使用MGLAnnotations而不是geoJSON源文件进行Mapbox聚类。 - Kerim Khasbulatov
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接