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)
}
}
}