Monday, July 1, 2024
HomeiOS DevelopmentDraw Arrow path for Location Based mostly Map navigation Utilizing ARKit iOS

Draw Arrow path for Location Based mostly Map navigation Utilizing ARKit iOS


I’ve indoor map and I need to draw path between two route location ex. from A to B I need to draw ARKit primarily based Arrow path in ios Utility. Presently I’m utilizing ARAnchor to attain this however challenges is that if A to B is 10 meter and I’m including Nodes on each meter so as an alternative of 10 completely different nodes i get single Arrow nodes displaying all 10 in it. I’m utilizing under code.

// Beneath Code from the place I’m calling addArpath perform

                    if let lat1 = mCurrentPosition?.latitude, let long1 = mCurrentPosition?.longitude {
                        
                        let latEnd = steplocation.latitude
                        let longEnd = steplocation.longitude
                        
                        // if let lastLat  = arrpath.final?.latitude,let lastLong = arrpath.final?.longitude,let lastAltitude = arrpath.final?.altitude{
                        
                        let userLocation = CLLocation(latitude: lat1, longitude: long1)
                        
                        let endLocation = CLLocation(coordinate: CLLocationCoordinate2DMake(CLLocationDegrees(latEnd), CLLocationDegrees(longEnd)), altitude: CLLocationDistance(steplocation.altitude), horizontalAccuracy: CLLocationAccuracy(5), verticalAccuracy: CLLocationAccuracy(0), course: CLLocationDirection(-1), velocity: CLLocationSpeed(5), timestamp: Date())
                        
                        let heading = getHeadingForDirectionFromCoordinate(from: userLocation, to: endLocation)

                        let lon1 =  degreesToRadians(long1)     //DegreesToRadians(long1)
                        let lon2 =  degreesToRadians(longEnd);  //DegreesToRadians(longEnd);
                        let lat2 =  degreesToRadians(latEnd);
                        

                        let dLon = lon2 - lon1
            
                        let y = sin(dLon) * cos(lat2);
                        yVal = yVal + y
                        
                       // let distanceToendpoint = calculateDistance(lat: endLocation.coordinate.latitude, lengthy: endLocation.coordinate.longitude)
                        let distvalue =  Int(distance) + Int(pathlength)
                        distance +=  CGFloat(distvalue)
                        
                       
                        for i in stride(from: 0, to: distance, by:1) {
                            print("present loop iteration is:" ,i)
                            let headingValue  = heading - self.heading
                            zValue = zValue + headingValue
                            
                            distanceVal = CGFloat(i) + distanceVal
                            zGlobal = zValue

                    // Calling addARPathtoLocation 

                            addARPathtoLocation(stepLocation:endLocation,zvalue: zValue, yvalue: yVal, distance:Float(i), path: manuoverType)
                        }
                       
                        //  }
                    }



 // MARK: - ARSessionDelegate
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    guard !(anchor is ARPlaneAnchor) else { return }
    let sphereNode = generateArrowNodes(anchor: anchor)
    DispatchQueue.most important.async {
        node.addChildNode(sphereNode)
    }
}

//create ARAnchor so as to add to nodes
func generateArrowNodes(anchor: ARAnchor) -> SCNNode {
    let imageMaterial = SCNMaterial()
    imageMaterial.isDoubleSided = true
    imageMaterial.diffuse.contents = UIImage(named: "blueArrow")
    
    let airplane = SCNPlane(width:0.5, peak:0.5)
    airplane.supplies = [imageMaterial]
    airplane.firstMaterial?.isDoubleSided = true
    
    let blueNode = SCNNode(geometry: airplane)
    blueNode.identify = "blueNode"
    blueNode.place = SCNVector3(x:Float(zGlobal), y:0, z:Float(distanceVal))
   
    blueNode.eulerAngles.x  = -.pi / 2
    blueNode.eulerAngles.y -= Float(CGFloat(CGFloat.pi/4*6))
    
    return blueNode
}

func addARPathtoLocation(stepLocation: CLLocation, zvalue: CGFloat, yvalue: CGFloat, distance:Float, path:VMEManeuverType) {

    // provide the depth of something ARKit has detected
    
    guard let question = sceneView.raycastQuery(from: sceneView.middle , permitting: .estimatedPlane, alignment: .any) else {
        return
    }
    
    let outcomes = sceneView.session.raycast(question)
    guard let hitResult = outcomes.first else {
        print("No floor discovered")
        return
    }
    
    // Add ARAnchor to Scene
    let anchor = ARAnchor(remodel: hitResult.worldTransform)
    sceneView.session.add(anchor: anchor)
}




func radiansToDegrees(_ radians: Double) -> Double {
    return (radians) * (180.0 / Double.pi)
}

func degreesToRadians(_ levels: Double) -> Double {
    return (levels) * (Double.pi / 180.0)
}

func getHeadingForDirectionFromCoordinate(from: CLLocation, to: CLLocation) -> Double {
    let fLat = degreesToRadians(from.coordinate.latitude)
    let fLng = degreesToRadians(from.coordinate.longitude)
    let tLat = degreesToRadians(to.coordinate.latitude)
    let tLng = degreesToRadians(to.coordinate.longitude)
    
    let deltaL = tLng - fLng
    
    let x =  sin(deltaL) * cos(tLng) //cos(tLat) * sin(deltaL)
    let y = cos(fLat) * sin(tLat) - sin(fLat) * cos(tLat) * cos(deltaL)
    
    let bearing = atan2(x,y)
    let bearingInDegrees = bearing.toDegrees
    print("Bearing Levels :",bearingInDegrees) // sanity examine
    

   // let diploma = radiansToDegrees(atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)))
    
    if bearingInDegrees >= 0 {
        return bearingInDegrees
    } else {
        return bearingInDegrees + 360
    }
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments