首页 > 解决方案 > 如何从 NSExpression 中提取 Int 值?

问题描述

我有一个带有一些集群点的地图视图(mapbox),集群是根据geojson排列和显示的,集群的逻辑就像它有邻居一样,当地图缩小时它们会集群。

这很好用,但是,我需要在 中提取值NSExpression,但我找不到解决方法。这种方法有什么常见的做法吗?

我使用这个例子作为我的基础。 https://docs.mapbox.com/ios/maps/examples/clustering/

我已经修改了这个,所以集群大小取决于集群中的点数,但是,当有很多点时,在 120 点范围内的某个地方,集群变得太大,所以我提取的原因point_count是我可以使用它设置一些规则。另外,如果您对我的问题有更好的解决方案,请分享。

    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")
        ports.iconAllowsOverlap = NSExpression(forConstantValue: true)

        style.addLayer(ports)

        // Color clustered features based on clustered point counts.
        let stops = [
            20: UIColor(red: 0, green: 124/255, blue: 1, alpha: 1),
            50: UIColor(red: 0, green: 124/255, blue: 1, alpha: 1)
        ]


        // Show clustered features as circles. The `point_count` attribute is built into
        // clustering-enabled source features.
        let circlesLayer = MGLCircleStyleLayer(identifier: "clusteredPorts", source: source)

        circlesLayer.circleOpacity = NSExpression(forConstantValue: 0.75)
        circlesLayer.circleStrokeColor = NSExpression(forConstantValue: UIColor.white.withAlphaComponent(1.0))
        circlesLayer.circleStrokeWidth = NSExpression(forConstantValue: 3)
        circlesLayer.circleColor = NSExpression(format: "mgl_step:from:stops:(point_count, %@, %@)", UIColor(red: 0, green: 124/255, blue: 1, alpha: 1), stops)


        circlesLayer.circleRadius = NSExpression(format: "CAST(point_count / 2, 'NSNumber')")

        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)

// THIS IS WHERE I WANT TO EXTRACT THE VALUE, from "point_count" to Int.
//        `numbersLayer.text = NSExpression(format: "CAST(point_count, 'NSString')")`


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

我期待从NSExpression(format: "CAST(point_count, 'NSNumber')")In中提取 point_count

标签: iosswiftmapboxnsexpressionmglmapview

解决方案


推荐阅读