我正在尝试在我触摸屏幕的平面上添加一个 GeoAnchor。我已经设置了 ARGeoTrackingConfiguration,如下所示:
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
let session = arView.session
let config = ARGeoTrackingConfiguration()
config.planeDetection = .horizontal
session.run(config)
// Start listening for location updates from Core Location
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
arView.addGestureRecognizer(UITapGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.onTap)))
context.coordinator.arView = arView
return arView
}
在我的协调员中,我执行以下操作:
@objc func onTap(_ recognizer: UITapGestureRecognizer) {
guard let arView = arView else {
return
}
let location = recognizer.location(in: arView)
let results = arView.raycast(from: location, allowing: .estimatedPlane, alignment: .any)
if let result = results.first {
arView.session.getGeoLocation(forPoint: result.worldTransform.translation) { coordinate, altitude, error in
print(coordinate)
let geoAnchor = ARGeoAnchor(coordinate: coordinate, altitude: altitude)
let anchor = AnchorEntity(anchor: geoAnchor)
let box = ModelEntity(mesh: MeshResource.generateBox(size: 0.3), materials: [SimpleMaterial(color: .green, isMetallic: true)])
anchor.addChild(box)
arView.scene.addAnchor(anchor)
}
}
}
getGeoLocation 函数总是返回坐标为 -180 和 -180,这是错误的。我所在的地区可以使用 GeoAnchors,但我不确定为什么它们不起作用。
In the documentation of arView.session.getGeoLocation it says "To succeed, this function requires an ARGeoTrackingConfiguration session with state equal to ARGeoTrackingStatus.State.localized".
我不确定应该在哪里设置 ARGeoTrackingStatus.State.localized。
任何帮助!
您必须等待
ARSession.currentFrame
报告 .localized
的 geoTrackingStatus。然后 getGeoLocation 就可以工作了。