AndrewHartAR / ARKit-CoreLocation

Combines the high accuracy of AR with the scale of GPS data.
MIT License
5.47k stars 739 forks source link

Add SwiftUI Support #187

Open intere opened 5 years ago

intere commented 5 years ago

With the latest release of Xcode 11 Beta, there is "SwiftUI". Ensure that we can support this.

CodyPChristian commented 4 years ago

Any chance of SwiftUI support?

andrewserff commented 4 years ago

You can wrap your ARCL ViewController in a UIViewControllerRepresentable. Here is an example:

import SwiftUI
import CoreLocation
import ARCL

struct ARCLView: View {
    var sceneLocationView = SceneLocationView()

    var body: some View {
        return ARCLViewContainer().edgesIgnoringSafeArea(.all)
    }
}

struct ARCLViewContainer: UIViewControllerRepresentable {

    func makeUIViewController(context: Context) -> ARCLViewController {
        return ARCLViewController()
    }

    func updateUIViewController(_ uiView: ARCLViewController, context: Context) {}

}

struct ARCLView_Previews: PreviewProvider {
    static var previews: some View {
        ARCLView()
    }
}

The ARCLViewController is simply like the examples:

class ARCLViewController: UIViewController {
...
}
BanTheRewind commented 4 years ago

Also using this in SwiftUI np. I have a view with a UIViewControllerRepresentable which creates a Coordinator that extends ARSessionDelegate, as well as a UIViewController. The controller allows me to read AR data in real time from the session and update my Environment object, which in turns passes values to the UIViewController from the Representable as my Environment object changes.

jacogasp commented 4 years ago

Hello guys, I successfully integrated the ARCL ViewController with UIViewControllerRepresentable. Did you find a way to listen to LNTouchDelegate events?

vindicatesociety commented 3 months ago

I'm new to ARKit and I having trouble getting the nodes to show up in SwiftUI, UIViewRepresentable. What am I missing to get them to show up along the route? 😩

func startNavigation(to destination: CLLocation, in sceneLocationView: SceneLocationView) {
        // 1. Get user's current location
        guard let currentLocation = sceneLocationView.sceneLocationManager.currentLocation else {
            print("Error: Unable to get current location.")
            return
        }

        // 2. Calculate route using MapKit
        let request = MKDirections.Request()
        request.source = MKMapItem(placemark: MKPlacemark(coordinate: currentLocation.coordinate))
        request.destination = MKMapItem(placemark: MKPlacemark(coordinate: destination.coordinate))
        request.transportType = .any 
        let directions = MKDirections(request: request)

        directions.calculate { response, error in
            if let route = response?.routes.first {
                // Extract route geometry
                let routePoints = route.polyline.points()
                let routeCoordinates = (0..<route.polyline.pointCount).map { routePoints[$0].coordinate }

                // Position the line nodes along the route

                for i in 0..<routeCoordinates.count - 1 {
                    let startLocation = CLLocation(latitude: routeCoordinates[i].latitude, longitude: routeCoordinates[i].longitude)
                    let endLocation = CLLocation(latitude: routeCoordinates[i + 1].latitude, longitude: routeCoordinates[i + 1].longitude)

                    // Create the nodes for start and end locations
                    let startNode = LocationNode(location: startLocation)
                    let endNode = LocationNode(location: endLocation)

                    // Create a line geometry between the points
                    let lineGeometry = SCNGeometry.line(from: [startNode.location, endNode.location])
                    let lineNode = SCNNode(geometry: lineGeometry)

                    // Set the color and thickness of the line
                    lineNode.geometry?.firstMaterial?.diffuse.contents = UIColor.red
                    lineNode.geometry?.firstMaterial?.isDoubleSided = true

                    // Add the line segment to the scene
                    sceneLocationView.addLocationNodeWithConfirmedLocation(locationNode: startNode)
                    sceneLocationView.addLocationNodeWithConfirmedLocation(locationNode: endNode)
                }
            } else if let error = error {
                print("Error calculating route: \(error.localizedDescription)")
            } else {
                print("No route found")
            }
        }
    }