1

I'm trying to build an AR App which places an object with ARSCNView Scene based upon its Longitude and Latitude.

I need to convert latitude and longitude values to a point in the 3-dimensional space relative to my phones latitude and longitude. I've been trying this for about 2 hours now, but I do not get the correct results.

Using CoreLocation I can find the lat/long of the phone when the AR Session starts and I know my location (lat\long) of the object I want to place. I'm assuming altitude is a constant 1m above ground and that I'm within at least 20m's of my object's physical location.

Here's my code:

func latlongToSCNVector3(phone: CLLocation, object: CLLocationCoordinate2D) -> SCNVector3 {
        
        let rad : Double = 6378.137 // Radius of the Earth (in kilometers)
        
        let xPhone = rad * cos(phone.coordinate.latitude) * cos(phone.coordinate.longitude)
        let yPhone = rad * cos(phone.coordinate.latitude) * sin(phone.coordinate.longitude)
        let zPhone = rad * sin(phone.coordinate.latitude)

        let xObject = rad * cos(object.latitude) * cos(object.longitude)
        let yObject = rad * cos(object.latitude) * sin(object.longitude)
        let zObject = rad * sin(object.latitude)
        
        let vector : SCNVector3 = .init( (xObject - xPhone), (yObject - yPhone), (zObject - zPhone) )
        return vector
        
}

My question is, do I need to convert the lat/long degrees into Radians and does the math look correct?

Thanks

duck1970
  • 127
  • 10

0 Answers0