Select to view content in your preferred language

How to use ENCMarinerSettings to set depth units with iOS/Swift

1644
15
Jump to solution
01-14-2024 08:23 AM
ChrisPelon
Emerging Contributor

Can someone help me set the depth units to feet? Here's my code, I am displaying the ENC chart successfully.

 

 

import SwiftUI
import ArcGIS

struct ContentView: View {
    @State private var map = AGSMap(basemapStyle: .arcGISOceans)

    var body: some View {
        MapView(map: map)
            .edgesIgnoringSafeArea(.all)
            .onAppear {
                map.initialViewpoint = AGSViewpoint(latitude: 45.318063, longitude: -85.2584, scale: 30055)
                loadENCExchangeSet()
            }
    }

    private func loadENCExchangeSet() {
        
        let depthUnits = AGSENCMarinerSettings.shared()
        

        // Load ENC Exchange Set
        if let catalogURL = Bundle.main.url(forResource: "CATALOG", withExtension: "031", subdirectory: "/ENC_ROOT") {
            let encExchangeSet = AGSENCExchangeSet(fileURLs: [catalogURL])
            
            encExchangeSet.load { error in
                if let error = error {
                    print("Error loading ENC Exchange Set: \(error.localizedDescription)")
                } else {
                    
                    let encLayers = encExchangeSet.datasets.map { AGSENCLayer(cell: AGSENCCell(dataset: $0)) }
                    DispatchQueue.main.async {
                        self.map.operationalLayers.addObjects(from: encLayers)
                        // Adjust this viewpoint to match where your ENC data is
                        self.map.initialViewpoint = AGSViewpoint(latitude: 45.318063, longitude: -85.2584, scale: 30055)
                    }
                }
            }
        } else {
            print("Error: CATALOG.031 file not found in bundle.")
        }
    }

}

struct MapView: UIViewRepresentable {
    let map: AGSMap
    let graphicsOverlay = AGSGraphicsOverlay()

    func makeUIView(context: Context) -> AGSMapView {
        let mapView = AGSMapView()
        mapView.map = map
        mapView.graphicsOverlays.add(graphicsOverlay)
        return mapView
    }

    func updateUIView(_ uiView: AGSMapView, context: Context) {
        // Perform any necessary updates to the UIView
    }

}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

 

 

0 Kudos
15 Replies
rolson_esri
Regular Contributor

The API has changed. Load doesn't take a completion block like the 100 SDK. The Swift SDK was redesigned from the ground up to take advantage of all the features of swift, including Swift Concurrency.  That means that loading an exchange set is now an async method:

 

try await encExchangeSet.load()

 

adding operational layers is also different. There are add methods directly on the map (see doc here) .

 

So that code should probably look something like:

self.map.addOperationalLayers(encLayers)

 

rolson_esri
Regular Contributor

BTW, as you migrate from 100 to 200 here is a doc that might help: https://developers.arcgis.com/swift/reference/migrate-from-100-x-to-200-x/

0 Kudos
ChrisPelon
Emerging Contributor

Thank you! This was the info I needed.

0 Kudos
rolson_esri
Regular Contributor

Can you please let me know if the depth units still don't work for you after you migrate your code? Thank you.

0 Kudos
ChrisPelon
Emerging Contributor

Depth units are now working as expected in 200 and the updated code. Thanks!

0 Kudos
rolson_esri
Regular Contributor

Also, @ChrisPelon, what version of 100.x are you using?

0 Kudos