1

如果这个问题不是那么好,请原谅我。我在 Apple 的 ARGeoAnchor 文档中遇到了一些障碍。

目前 ARGeoAnchor 仅在 AR 场景视图中显示一个蓝点。我正在尝试显示任何 3d 渲染或对象。

我的代码:

let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lng)
let geoAnchor = ARGeoAnchor(name: "Point 1", coordinate: coordinate)
    
let boxGeometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
let cube = SCNNode(geometry: boxGeometry)
geoAnchor.scene.rootNode.addChildNode(cube)
self.addGeoAnchor(geoAnchor)

我得到的错误:Value of type 'ARGeoAnchor' has no member 'scene'

我有多个 ARGeoAnchors,它们目前都显示蓝点。如何让他们改为显示自定义 3d 对象?

谢谢参观!

4

1 回答 1

1

首先,您必须检查ARGeoTrackingConfiguration您的设备是否支持:

import ARKit

@main class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, 
                       didFinishLaunchingWithOptions launchOptions: 
                                      [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        if !ARGeoTrackingConfiguration.isSupported {

            let sb = UIStoryboard(name: "Main", bundle: nil)

            window?.rootViewController = sb.instantiateViewController(withIdentifier:
                                                             "unsupportedConfiguration")
        }
        return true
    }
}

在此处输入图像描述

...然后检查您所在位置是否可以使用地理跟踪:

ARGeoTrackingConfiguration.checkAvailability { (available, error) in

    if !available {
        let errorDescription = error?.localizedDescription ?? ""
        let recommendation = "You need a place where geo tracking is supported."
        let restart = UIAlertAction(title: "Restart", style: .default) { (_) in
            self.restartSession()
        }
        self.alertUser(withTitle: "Geo tracking unavailable",
                         message: "\(errorDescription)\n\(recommendation)",
                         actions: [restart])
    }
}

之后提供Info.plist相机位置许可以下是有关位置服务授权请求的综合信息,其中包括:

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysAndWhenInUseUsageDescription
  • NSLocationUsageDescription
  • NSLocationAlwaysUsageDescription


现在只支持美国城市和英国首都......

支持 ARGeoTrackingConfiguration 的城市列表


然后您必须运行地理配置:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    let config = ARGeoTrackingConfiguration()
    sceneView.session.run(config)
}

然后将参数化锚点添加到会话中:

@IBOutlet var sceneView: ARSCNView!

override func viewDidLoad() {
    super.viewDidLoad()
    sceneView.delegate = self
    sceneView.scene = SCNScene()
    
    let coordinate = CLLocationCoordinate2D(latitude: 40.730610, 
                                           longitude: -73.935242)

    let geoAnchor = ARGeoAnchor(name: "Geo Anchor",
                          coordinate: coordinate,
                            altitude: 33.0)
    
    sceneView.session.add(anchor: geoAnchor)
}

之后,您可以在 ARGeoAnchor 的帮助下添加模型:

extension ViewController: ARSCNViewDelegate {
    
    func renderer(_ renderer: SCNSceneRenderer,
                 didAdd node: SCNNode,
                  for anchor: ARAnchor) {
            
        guard let geoAnchor = anchor as? ARGeoAnchor,
                  geoAnchor.name == "Geo Anchor"
        else { return }
        
        print(geoAnchor.coordinate)
                
        let boxGeometry = SCNBox(width: 1.0,
                                height: 1.0,
                                length: 1.0,
                         chamferRadius: 0.1)

        boxGeometry.firstMaterial?.diffuse.contents = UIColor.red

        let cube = SCNNode(geometry: boxGeometry)
        
        node.addChildNode(cube)
    }
}
于 2020-12-08T14:24:23.897 回答