ios - 如何将在 map 上重叠的 MGLAnnotations 组合在一起?

标签 ios swift mapbox

我正在为 ios 使用 mapbox map 。在其中,我添加了注释。当您开始缩小时,注释会越来越接近它们开始重叠的程度。我一直在尝试编写逻辑来解决这个问题,但这会使应用程序变慢。

我想知道是否有一些内置的 mapbox 方法可以处理我的这种[分组]。

是这样吗?可以分享吗?

如果有帮助,下面是我一直在尝试的代码:

    func mapViewRegionIsChanging(_ mapView: MGLMapView) {
    print(mapView.zoomLevel, " Cenetr -<<<<")
    print(mapView.visibleAnnotations, " These are the visible annotations")
    grouping(mapView: mapView)
}

func grouping(mapView: MGLMapView) {

    if let annotations = mapView.visibleAnnotations {

        var lastVal = 0.0
        var arrayAnnotationsToRemove = [CustomPointAnnotation]()
        var arrayPolylinesToRemove = [MGLPolyline]()

        for val in arrayOfLineInformationObjects {

            if mapView.zoomLevel < 10 && mapView.zoomLevel > 7 {
                let dif = lastVal-val.arrayOfPointAnnotations![0].coordinate.longitude
                print(dif, "The dif is to teh left")
                //long is being compared
                if dif <= 0.01 {//dif >= 0.05 &&
                    print("Delete")
                    arrayAnnotationsToRemove.append(val.arrayOfPointAnnotations![0])
                    arrayAnnotationsToRemove.append(val.arrayOfPointAnnotations![1])
                    arrayPolylinesToRemove.append(val.polyline!)
                }

            } else if mapView.zoomLevel < 7 && mapView.zoomLevel > 5 {
                let dif = lastVal-val.arrayOfPointAnnotations![0].coordinate.longitude
                print(dif, "The dif is to teh left")
                //long is being compared
                if dif <= 0.01 {
                    print("Delete 2")
                    arrayAnnotationsToRemove.append(val.arrayOfPointAnnotations![0])
                    arrayAnnotationsToRemove.append(val.arrayOfPointAnnotations![1])
                    arrayPolylinesToRemove.append(val.polyline!)
                }
            }
            lastVal = val.arrayOfPointAnnotations![0].coordinate.longitude
        }
        for (valueA, valueP) in zip(arrayAnnotationsToRemove, arrayPolylinesToRemove) {
            mapView.removeAnnotation(valueA)
            mapView.removeAnnotation(valueA)
            mapView.remove(valueP)
        }
        if arrayAnnotationsToRemove.count > 0 {
            let newGroupedAnnotation = MGLPolygon(coordinates: &arrayAnnotationsToRemove[0].coordinate, count: 1)
            mapView.addAnnotation(polygonCircleForCoordinate(coordinate: arrayAnnotationsToRemove[0].coordinate, withMeterRadius: 50))
        }
        //notation(T##annotation: MGLAnnotation##MGLAnnotation)
    }
}

最佳答案

您可以为此使用聚类点数据

您可以找到它的详细示例here .

我在这里复制了didFinishLoading函数,以防以后链接断开

func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
    let url = URL(fileURLWithPath: Bundle.main.path(forResource: "ports", ofType: "geojson")!)

    let source = MGLShapeSource(identifier: "clusteredPorts",
        url: url,
        options: [.clustered: true, .clusterRadius: icon.size.width])
    style.addSource(source)

    // Use a template image so that we can tint it with the `iconColor` runtime styling property.
    style.setImage(icon.withRenderingMode(.alwaysTemplate), forName: "icon")

    // Show unclustered features as icons. The `cluster` attribute is built into clustering-enabled
    // source features.
    let ports = MGLSymbolStyleLayer(identifier: "ports", source: source)
    ports.iconImageName = NSExpression(forConstantValue: "icon")
    ports.iconColor = NSExpression(forConstantValue: UIColor.darkGray.withAlphaComponent(0.9))
    ports.predicate = NSPredicate(format: "cluster != YES")
    style.addLayer(ports)

    // Color clustered features based on clustered point counts.
    let stops = [
    20: UIColor.lightGray,
    50: UIColor.orange,
    100: UIColor.red,
    200: UIColor.purple
    ]

    // Show clustered features as circles. The `point_count` attribute is built into
    // clustering-enabled source features.
    let circlesLayer = MGLCircleStyleLayer(identifier: "clusteredPorts", source: source)
    circlesLayer.circleRadius = NSExpression(forConstantValue: NSNumber(value: Double(icon.size.width) / 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")
    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: "clusteredPortsNumbers", source: source)
    numbersLayer.textColor = NSExpression(forConstantValue: UIColor.white)
    numbersLayer.textFontSize = NSExpression(forConstantValue: NSNumber(value: Double(icon.size.width) / 2))
    numbersLayer.iconAllowsOverlap = NSExpression(forConstantValue: true)
    numbersLayer.text = NSExpression(format: "CAST(point_count, 'NSString')")

    numbersLayer.predicate = NSPredicate(format: "cluster == YES")
    style.addLayer(numbersLayer)
    }

关于ios - 如何将在 map 上重叠的 MGLAnnotations 组合在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54798359/

相关文章:

ios - 如何在核心数据中找到属性的索引

ios - 在没有 segue 的情况下在多个 View Controller 之间共享数据(Swift 3)

ios - URLSession 不会在 header swift 4 中传递 'Authorization' 键

iphone - 内容定位到 UIScrollView 中

swift - 枚举类的初始化和将TemperatureUnit.Kelvin分配给self(self = .Kelvin)的困惑

ios - NSPersistentContainer 不创建数据库文件

leaflet - 如何使用超集群

安卓 map 框 : How to unregister IntentReceiver?

ios - 如何在 IBAction 按钮中初始化 Tile 源一次

ios - 在另一个自定义 ViewController 中更改自定义 UIView 的变量