World latitude/longitude to scene Vector3 conversion

I want to place 3d objects to a sphere(planet).
I have the real latitude/longitude coordinates to which I want to place my 3d objects, but I need their babylonJS Vector3 representation. How can I do that?
I think there are also many other people looking at a solution to this problem.

I was unable to find any demo/code to convert both ways:
babylonJS coordinates of a point at a sphere to latitude/longitude(real world GPS coordinates)

Basically I need an implementation to LocationService interface.
I think I need to pass the mesh or the radius of the 3d object.

export interface LocationService
   getVector3(coordinates: GpsCoordinates): Vector3;
   getCoordinates(position: Vector3): GpsCoordinates;
export interface GpsCoordinates
  latitude: number;
  longitude: numberl

What I need visually:

That’s not my topic, but I want exactly the same in BabylonJS:

1 Like

i reseached how to do it once , but it was for blender python solution , anyway if you know code then you could port it to javascript :

def llarToWorld(lat, lon, alt, rad):
    # see:
    f  = 0                              # flattening
    ls = math.atan((1 - f)**2 * math.tan(lat))    # lambda

    x = rad * math.cos(ls) * math.cos(lon) + alt * math.cos(lat) * math.cos(lon)
    y= rad * math.cos(ls) * math.sin(lon) + alt * math.cos(lat) * math.sin(lon)
    z = rad * math.sin(ls) + alt * math.sin(lat)

    return (x, y, z)

d = "Moscow,55.755826,37.6173,London,51.5073509,-0.1277583,Paris,48.856614,2.3522219,New York,40.7127837,-74.0059413,Saint Petersburg,27.7518284,-82.6267345"

da = d.split(",");
vd = []
vd_names = []
i = 0

while i < len(da):
    i += 3

Thank you @shaderbytes ,
This will be the first thing early in the morning to check whether it works.

i could post you a online version of this function at work in an art piece ( code not executing in runtime , was used to build the model and animations. Anyway it i from a competing 3D web solution (sketchfab) so I dont think I should place the link in here.

Hope you get is sorted out :wink:

I ported it, no issues there, but the result is not what I am expecting.
I think it does not work as expected.
JavaScript code looks like:

        var llarToWorld = function(lat, lon, alt, radius) {
            f  = 0;
            ls = Math.atan((1 - f)**2 * Math.tan(lat));
            x = radius * Math.cos(ls) * Math.cos(lon) + alt * Math.cos(lat) * Math.cos(lon);
            y = radius * Math.cos(ls) * Math.sin(lon) + alt * Math.cos(lat) * Math.sin(lon);
            z = radius * Math.sin(ls) + alt * Math.sin(lat);

            return ({x, y, z}) 

I need BabylonJS representation of:

var paris = {
  lat: 48.864716,
  lon: 2.349014

var parisSpherical = {
  lat: THREE.Math.degToRad( 90 - ),
  lon: THREE.Math.degToRad( paris.lon )

var radius = 10;

var parisVector = new THREE.Vector3().setFromSphericalCoords( radius,, parisSpherical.lon);
// check we did it correctly
var spherical = new THREE.Spherical().setFromVector3(parisVector);

This function helped me to solve my question.

var getCoordinatesFromLatLng = function(latitude, longitude, radiusEarth)
   let latitude_rad = latitude * Math.PI / 180;
   let longitude_rad = longitude * Math.PI / 180;

   let xPos= radiusEarth * Math.cos(latitude_rad) * Math.cos(longitude_rad);
   let zPos = radiusEarth * Math.cos(latitude_rad) * Math.sin(longitude_rad);
   let yPos = radiusEarth * Math.sin(latitude_rad);
   return {x: xPos, y: yPos, z: zPos};

I used this exact code and it worked … did you notice when I called it , was converted to radians


you probably overlooked that part :wink:

@shaderbytes , yeah, you are right, I missed that part.
Anyways, thank you!