When the user taps the Done button, the AR view is dismissed and the distance view is shown again. Then the user should walk to another place and pass the iPhone to another player. This player can then use the distance information to find the text location. In this section, we’ll program our app to show the stored world map when the user is within 10 meters of the stored location and can start searching the ground for the text using the AR view.
We already have a method that presents the AR view to the user. But we need to tell the ARViewController instance whether it should load the stored world map. Add the following property to ARViewController:
| var shouldRestore = false |
We’ll use this property later to decide whether we should load a stored world map or start with a new one.
Now open DistanceViewController.swift and change showAR so that it looks like the following code. The lines that have changed are highlighted.
» | func showAR(shouldRestore: Bool = false) { |
| |
| if let next = storyboard?.instantiateViewController( |
| withIdentifier: "ARViewController") as? ARViewController { |
| |
» | next.shouldRestore = shouldRestore |
| next.modalPresentationStyle = .fullScreen |
| present(next, animated: true, completion: nil) |
| } |
| } |
| } |
We added the optional parameter shouldRestore. The value of this parameter is passed to the instance of ARViewController.
When the distance to the stored location is less than 10 meters, we want to present the AR view. Add the highlighted code in updateUI(for:) right after the code lastDistance = distance:
| func updateUI(for location: CLLocation?) { |
| |
| guard let location = location else { |
| return |
| } |
| |
| if let storedLocation = storedLocation { |
| |
| let distance = location.distance(from: storedLocation) |
| distanceLabel.text = String(format: "%.2lf m", distance) |
| |
| if lastDistance < distance { |
| view.backgroundColor = .red |
| } else if lastDistance > distance { |
| view.backgroundColor = .green |
| } |
| |
| lastDistance = distance |
| |
» | if distance < 10 { |
» | showAR(shouldRestore: true) |
» | } |
| } else { |
| let coordinate = location.coordinate |
| distanceLabel.text = String(format: "%.6lf, %.6lf", |
| coordinate.latitude, |
| coordinate.longitude) |
| } |
| } |
Make sure that the latitude of the custom location is set to 1.0001 and the longitude is set to 2. Then build and run the app on the simulator. The app should show the distance view with a distance of 11.06 m. Next, change the latitude to 1. Our app shows the AR view.
This works—nice. But there’s a problem with this implementation. When the user taps the Done button, the AR view is dismissed. But since the user is still within 10 meters of the stored location, the AR view is immediately presented again.
Let’s add some code to ensure that the AR view won’t be shown again until the user leaves the 10-meter radius. Add the following property to DistanceViewController:
| private var stillInRadius = false |
When the AR view is presented to the user, we set this property to true. Add the highlighted line at the beginning of showAR(shouldRestore:):
| func showAR(shouldRestore: Bool = false) { |
| |
» | stillInRadius = true |
| |
| if let next = storyboard?.instantiateViewController( |
| withIdentifier: "ARViewController") as? ARViewController { |
| |
| next.shouldRestore = shouldRestore |
| next.modalPresentationStyle = .fullScreen |
| present(next, animated: true, completion: nil) |
| } |
| } |
Replace the code we added to updateUI(for:) to present the AR view with the following code. The changed lines are highlighted.
| func updateUI(for location: CLLocation?) { |
| |
| guard let location = location else { |
| return |
| } |
| |
| if let storedLocation = storedLocation { |
| |
| let distance = location.distance(from: storedLocation) |
| distanceLabel.text = String(format: "%.2lf m", distance) |
| |
| if lastDistance < distance { |
| view.backgroundColor = .red |
| } else if lastDistance > distance { |
| view.backgroundColor = .green |
| } |
| |
| lastDistance = distance |
| |
| if distance < 10 { |
» | if stillInRadius == false { |
| showAR(shouldRestore: true) |
» | } |
» | } else { |
» | stillInRadius = false |
| } |
| } else { |
| let coordinate = location.coordinate |
| distanceLabel.text = String(format: "%.6lf, %.6lf", |
| coordinate.latitude, |
| coordinate.longitude) |
| } |
| } |
In this code we check if the property stillInRadius is false. Only then do we present the AR view when the user is within 10 meters of the stored location. We also set the property to false when the user is outside of the 10-meter radius. This should do the trick. In the next section we’ll load the stored world map from the documents directory.