Sparsh gupta
Sparsh gupta

Reputation: 31

Compass Heading information in RealityKit

I'm new to SwiftUI, and I've learned some basics of my project's RealityKit and ARKit. I just want to display an arrow that always faces towards the north, or at least get heading information displayed as text when I open the camera (AR Experience).

Waiting for someone to solve this fundamental problem.

Thanks in advance!

Upvotes: 0

Views: 521

Answers (1)

Andy Jazz
Andy Jazz

Reputation: 58103

Use the following code to create AR experience that depends on device's geo position.

Reality Composer

enter image description here

Code

import SwiftUI
import RealityKit
import ARKit

struct ARViewContainer: UIViewRepresentable {
       
    func makeUIView(context: Context) -> ARView {
        let arView = ARView(frame: .zero)
        arView.cameraMode = .ar
        arView.automaticallyConfigureSession = false

        let config = ARWorldTrackingConfiguration()
        config.worldAlignment = .gravityAndHeading       // case = 1
        arView.session.run(config)
        
        let arrowScene = try! Experience.loadNorth()
        arView.scene.anchors.append(arrowScene)
        return arView
    }
    
    func updateUIView(_ uiView: ARView, context: Context) { }
}

struct ContentView : View {
        
    var body: some View {
        ZStack {
            ARViewContainer().ignoresSafeArea()
        }
    }
}

Settings

On device, go to SettingsPrivacyLocation ServicesOn. After that, in Xcode, append Privacy LocationUsageDescription and LocationWhenInUseUsageDescription and then CameraUsageDescription keys in info tab.

enter image description here

Upvotes: 1

Related Questions