Sign up for our live Camera Kit office hour on Dec 13 @ 9:30am (PT) / 4:30pm (GMT)

Quick Tip: Geolocated Lens, Compass Heading, Magnetic North

aidan Posts: 32 🔥
edited October 31 in General #1

Hi all, I learned if you wait for the ARSession to be ready you can update its configuration information! This means you can set worldAlignment to .gravityAndHeading so your Lens lines up with true North instead of the arbitrary orientation of your phone which opens up the possibility to do map-based AR applications. I've added the code to CustomizedCameraController.swift

@State var arSessionInput = ARSessionInput();
     input: avSessionInput,
     arInput: arSessionInput,
     cameraPosition: .back,
     videoOrientation: orientation,
     dataProvider: self.configureDataProvider(),
     hintDelegate: self,
     textInputContextProvider: textInputContextProvider,
     agreementsPresentationContextProvider: agreementsPresentationContextProvider

func waitForSession() {
     if arSessionInput.session.currentFrame != nil {
          print("session ready!");
          let newConfiguration = ARWorldTrackingConfiguration()
          newConfiguration.worldAlignment = .gravityAndHeading
     } else {
          DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {

It's a rough workaround but it gets the result! FYI you may have to setup location services and other permissions and settings beforehand for this to work.


  • aidan
    aidan Posts: 32 🔥

    Bonus: if you'd like to get your Lens camera's x,y,z coordinates you can do so with the following. I'm using this to update the user's geolocation with finer precision

    func getARPosition () {
            if let currentFrame = self.arSessionInput.session.currentFrame {
                let cameraTransform =
                let cameraPosition = cameraTransform.columns.3
                let x = cameraPosition.x
                let y = cameraPosition.y
                let z = cameraPosition.z
                print("Camera position: (\(x), \(y), \(z))")
            } else {
                print("Unable to access current frame")
  • arashp
    arashp Posts: 47 🔥

    Thank you for your awesome post @aidan. I am guessing it would be useful to get a callback when ARSession is ready so that the app doesn't have to wait and poll?

  • aidan
    aidan Posts: 32 🔥

    @arashp possibly! I think what would be more useful in this specific instance is the ability to input custom settings into ARSessionInput(session:ARSession) before initializing CameraKit. Maybe it's possible but I find it overrides the values I set

  • arashp
    arashp Posts: 47 🔥

    Got it, thanks again @aidan

  • Hey. Just seeing this post and I think it could really help with a problem I'm trying to solve. Any chance you could walk through how you did this?