首页 > 解决方案 > ESRI IOS 应用程序开发 - 在绘图类型之间切换时让“sketchEditor.start(with:”保留以前绘制的图形

问题描述

我是 Swift 新手,正在使用 ArcGIS Runtime for IOS 构建一个 GIS 应用程序以了解更多信息。该应用程序的一部分是让某人能够在地图上绘制临时图形。现在,当我在点/折线/多边形之间切换时,先前绘制的图形都会被删除,我想知道是否有办法将它们保留在那里,直到单击“清除”按钮。

我相信答案在于 self.sketchEditor.start(with: nil, creationMode: [draw style])

“nil”正在清除之前的内容,但我无法弄清楚我需要用什么替换它才能让它保留任何当前绘制的图形。

以下是我的代码,任何帮助将不胜感激。

//
//  ViewController.swift
//  [name]
//
//  Created by 
//  
//

import UIKit
import ArcGIS
import MessageUI

class ViewController: UIViewController {

    @IBOutlet weak var mapView: AGSMapView!
    @IBOutlet weak var geometrySegmentedControl: UISegmentedControl!
    @IBOutlet weak var undoBBI: UIButton!
    @IBOutlet weak var clearBBI: UIButton!

    private var map: AGSMap!
    private var sketchEditor: AGSSketchEditor!

    private func setupMap() {
        let itemID = [portal map]
        let portal = AGSPortal(url: URL(string: "https://www.arcgis.com")!, loginRequired: false)
        let portalItem: AGSPortalItem = AGSPortalItem(portal: portal, itemID: itemID)
        mapView.map = AGSMap(item: portalItem)
    }

    private func showAlert(withStatus: String) {
        let alertController = UIAlertController(title: "Alert", message:
            withStatus, preferredStyle: UIAlertController.Style.alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertAction.Style.default,handler: nil))
        present(alertController, animated: true, completion: nil)
    }

    func setupLocationDisplay() {
        mapView.locationDisplay.autoPanMode = AGSLocationDisplayAutoPanMode.compassNavigation
        mapView.locationDisplay.start { [weak self] (error:Error?) -> Void in
            if let error = error {
                self?.showAlert(withStatus: error.localizedDescription)
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        setupMap()

        setupLocationDisplay()

        let itemID = [portal]
        let portal = AGSPortal(url: URL(string: "https://www.arcgis.com")!, loginRequired: false)
        let portalItem: AGSPortalItem = AGSPortalItem(portal: portal, itemID: itemID)
        self.map = AGSMap(item: portalItem)

        self.sketchEditor = AGSSketchEditor()
        self.mapView.sketchEditor = self.sketchEditor

        self.sketchEditor.start(with: nil, creationMode: .unset)

        self.mapView.map = self.map
        self.mapView.interactionOptions.isMagnifierEnabled = false

        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.respondToGeomChanged), name: .AGSSketchEditorGeometryDidChange, object: nil)
    }

    @objc
    func respondToGeomChanged() {
        //Enable/disable UI elements appropriately
        self.undoBBI.isSelected = self.sketchEditor.undoManager.canUndo
        self.clearBBI.isSelected = self.sketchEditor.geometry != nil && !self.sketchEditor.geometry!.isEmpty
    }

    // MARK: - Actions

    @IBAction func geometryValueChanged(_ geometrySegmentedControl: UISegmentedControl) {
        switch geometrySegmentedControl.selectedSegmentIndex {
        case 0://multipoint
            self.sketchEditor.start(with: nil, creationMode: .unset)

        case 1://freehand polyline
            self.sketchEditor.start(with: nil, creationMode: .freehandPolyline)

        case 2://freehand polygon
            self.sketchEditor.start(with: nil, creationMode: .freehandPolygon)

        case 3://multipoint
            self.sketchEditor.start(with: nil, creationMode: .multipoint)

        default:
            break
        }

        self.mapView.sketchEditor = self.sketchEditor
    }

    @IBAction func undo() {
        if self.sketchEditor.undoManager.canUndo { //extra check, just to be sure
            self.sketchEditor.undoManager.undo()
        }
    }

    @IBAction func clear() {
        self.sketchEditor.clearGeometry()
    }
}

标签: iosswiftgisesri

解决方案


推荐阅读