RealityKit ARGeoAnchor Now Showing

I am trying to place a ARGeoAnchor right outside my house. I have hard-coded the coordinates but I cannot see the anchor. I believe the anchor should just show up even though I am sitting inside the house. Here is my implementation. It should show a box virtual object but I don't see anything.


import SwiftUI
import RealityKit
import MapKit
import ARKit

struct ContentView : View {
  var body: some View {
     
    
    return ARViewContainer().edgesIgnoringSafeArea(.all)
  }
}

struct ARViewContainer: UIViewRepresentable {
   
  init() {
     
    guard ARGeoTrackingConfiguration.isSupported else { return }
     
    ARGeoTrackingConfiguration.checkAvailability { available, error in
      guard available else { return }
       
      print(available)
    }
  }
   
  func makeUIView(context: Context) -> ARView {
     
    let arView = ARView(frame: .zero)
    arView.session.delegate = context.coordinator
    context.coordinator.arView = arView
    arView.session.run(ARGeoTrackingConfiguration())
     
    let latitude = 444 // put correct value here
    let longitude = 444 // put correct value here

    //let coordinate = CLLocationCoordinate2D(latitude: 29.95681449118455, longitude: -95.50191902521054)
    let coordinate = CLLocationCoordinate2D(latitude: 29.95685683319887, longitude: -95.50187836180176)
    let geoAnchor = ARGeoAnchor(coordinate: coordinate)
    arView.session.add(anchor: geoAnchor)
     
     
    return arView
  }
   
  func makeCoordinator() -> Coordinator {
    Coordinator()
  }
   
  func updateUIView(_ uiView: ARView, context: Context) {}
}

class Coordinator: NSObject, ARSessionDelegate {
   
  weak var arView: ARView?
   
  func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
     
    if let anchor = anchors.first {
       
      print(anchor)
      print("adding anchor")
      let box = ModelEntity(mesh: MeshResource.generateBox(size: 0.5), materials: [SimpleMaterial(color: .blue, isMetallic: true)])
       
      let geoAnchorEntity = AnchorEntity(anchor: anchor)
      geoAnchorEntity.addChild(box)
      //box.generateCollisionShapes(recursive: true)
       
      arView!.scene.addAnchor(geoAnchorEntity)
    }
     
  }
}

Replies

Hi, it looks like you're not checking if the GeoTracking sessions localizes (it has to go to ARGeoTrackingStatus.State.localized before GeoAnchors are updated). To get started I recommend compiling the example project:

https://developer.apple.com/documentation/arkit/content_anchors/tracking_geographic_locations_in_ar

That sample app will give you feedback if you're localizing and if not. There's also a lot of helpful tips in the ARKit 4 and 5 sessions on that topic:

https://developer.apple.com/videos/play/wwdc2020/10611/

https://developer.apple.com/videos/play/wwdc2021/10073/