Swift地图更改点(如果更改SegmentedControl)

时间:2018-08-27 07:18:28

标签: ios swift mapkit mapbox

我正在尝试使用Mapbox。

我在地图上创建了一个点并从文件中显示

我希望更改SegmentedControl后,item.type也相应地更改并获取正确的数据

例如,如果按0 case-> make 0 == item.type

我创建自定义点

func addItemsToMap(features: [MGLPointFeature]) {

    guard let style = mapView.style else { return }
    let source = MGLShapeSource(identifier: "mapPoints", features: features, options: nil)
    style.addSource(source)

    let colors = [
            "black": MGLStyleValue(rawValue: UIColor.black)
    ]
    let circles = MGLCircleStyleLayer(identifier: "mapPoints-circles", source: source)
    circles.circleColor = MGLSourceStyleFunction(interpolationMode: .identity,
                                                     stops: colors,
                                                     attributeName: "color",
                                                     options: nil)
    circles.circleRadius = MGLStyleValue(interpolationMode: .exponential,
                                             cameraStops: [2: MGLStyleValue(rawValue: 5),
                                                           7: MGLStyleValue(rawValue: 8)],
                                             options: nil)
    circles.circleStrokeWidth = MGLStyleValue(rawValue: 2)
    circles.circleStrokeColor = MGLStyleValue(rawValue: UIColor.white)

    style.addLayer(circles)
}

将数据放入地图框

func test(number: Int) {

    guard let documentsDirectoryUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
    let urlBar = documentsDirectoryUrl.appendingPathComponent("Persons.json")

    do {
        let jsonData = try Data(contentsOf: urlBar)
        //Переводим их в модель
        let result = try JSONDecoder().decode(CollectionTest.self, from: jsonData)
        let coll: CollectionTest = result
        let features = parseJSONItems(collection: coll, number: number)

        addItemsToMap(features: features)
        print(features)
    } catch { print("Error while parsing: \(error)") }
}

从文件获取数据

func parseJSONItems(collection: CollectionTest, number: Int) -> [MGLPointFeature] {

    var features = [MGLPointFeature]()
    for item in collection.prices {
            if item.type == number {
...get data to annotation and location
let feature = MGLPointFeature()
            feature.coordinate = coordinate
            feature.title = "\(name)"
            feature.attributes = [
                "color" : color,
                "name" : "\(name)"
            ]
            features.append(feature)
        }
                    }
                }}}
    }
    return features
}

我需要将number0更改为4,因为在数据中,我将类型从0更改为4,并将点数从{ {1}}-0(如果更改了4

我有SegmentedControl

SegmentedControl

在viewDidLoad中:

@objc func change(sender: UISegmentedControl) {
    switch sender.selectedSegmentIndex {
        case 0:
        test(number: 0)
        case 1:
        test(number: 1)
        case 2:
        ttest(number: 2)
        case 3:
        test(number: 3)
        case 4:
        test(number: 4)
        default:
        test(number: 2)
    }
}

当应用运行时-运行良好并显示所有数据==类型2

但是当我将override func viewDidLoad() { super.viewDidLoad() styleToggle.addTarget(self, action: #selector(change(sender:)), for: .valueChanged) test(number: 2) } 中的按钮更改为SegmentedControl或其他按钮时,会崩溃,并出现在控制台打印中

  

由于未捕获的异常而终止应用程序
  “ MGLRedundantSourceIdentifierException”,原因:“源mapPoints已经存在”

我在做什么错?该如何解决?

2 个答案:

答案 0 :(得分:1)

您似乎正在尝试将源添加到已经存在的地图中。为了避免出现此消息,您将需要:

  1. 在将源添加到样式之前,检查源是否存在。在您的用例中,也许将源命名为mapPoints-circles-\(number)。如果源已经存在,请重新使用它。

  2. 一次将源添加到地图(最好在首字母-mapView:didFinishLoadingStyle:内)。为了从其图层创建新图层,您可以从地图样式访问源。

如果这些点来自标识为my-source的来源,则可以尝试:

guard let source = style.source(withIdentifier: "my-source") else { return }

答案 1 :(得分:0)

点击路段时,删除地图中已经存在的所有标记 然后添加新标记

您可以使用map.removeLayer(marker);删除标记(ILayer对象)。

相关问题