Adding 3D object to ARGeoAnchor
At first you have to check if ARGeoTrackingConfiguration
is supported on your device.
Requirements: You need a device with A12+ chip and cellular (GPS) support.
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
}
}
...then check whether Geo Tracking is available at your location:
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])
}
}
After that supply Info.plist
with camera and location permission. Here's a comprehensive information on Authorization Request for Location Services what includes:
- NSLocationWhenInUseUsageDescription
- NSLocationAlwaysAndWhenInUseUsageDescription
- NSLocationUsageDescription
- NSLocationAlwaysUsageDescription
Only several cities in US, UK, Australia, Canada, Japan and Singapore are supported at the moment...
A list of cities that support ARGeoTrackingConfiguration.
Then you must run Geo Configuration:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let config = ARGeoTrackingConfiguration()
sceneView.session.run(config)
}
Then add parametrised anchor to the session:
@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)
}
And after that you can add a model with the help of 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)
}
}
P.S.
If you're interested in how a similar feature works in ARCore, read my post about Geospatial API.
Testing ARGeoTrackingConfiguration in unsupported area
Nope.
Supplementary visual data must be downloaded from Apple's server along with GPS coordinates. If your GPS coordinates are from unsupported area – there's no ARGeoAnchor
.
It's even impossible if you cheat GPS. Apple uses large LiDARs to digitize surrounding environments in big cities. You need to feed the same views thru rear camera, not only longitude and latitude. So, you have to physically be in ARGeoTracking-supported place. As an alternative you might use a replay data feature.
For additional info read this post and this post.
Why ARGeoTrackingConfiguration is not available everywhere?
That's because ARGeoTrackingConfig
doesn't use Google Maps. Apple runs its own LiDAR-equipped vehicles for digitizing cities to supply Apple Maps with all necessary information stored on Apple servers. That info contains precise GPS coordinates and Machine Learning mlmodel
that visually recognises every pre-digitized location. And that specific info is indispensable when you're running ARKit's app with geo anchors.
At the moment only several US cities are available for seeding ARGeoAnchors. However, in 2021 some European and Asian cities will be available for ARKit's geo anchoring as well.
Look at this post to find out how to implement ARGeoTrackingConfig
.
Related Topics
Swift Spritekit Adding Button Programmatically
What Is the Reduce() Function Doing, in Swift
In Swift 3, What Is a Way to Compare Two Closures
Any Way to Iterate a Tuple in Swift
How to Include .Swift File from Other .Swift File in an Immediate Mode
Different Ways to Initialize a Dictionary in Swift
Binary Operator '/' Cannot Be Applied to Two 'Double' Operands
Transparent Background for Texteditor in Swiftui
In Swift, How to Cast to Protocol with Associated Type
Retrieving Image from Firebase Storage Using Swift
Finish Asynchronous Task in Firebase With Swift
Convert a Swift Array of String to a to a C String Array Pointer
Why Is There No Universal Base Class in Swift
Nsimage to Nsdata as Png Swift
How Does One Trap Arithmetic Overflow Errors in Swift
Swift Date(Byadding:To:) Returns Nil for Trivial Calculation in Repl