1

I am doing a Javascript application that requires the movement of a certain element in a real world map, in frames.

For each frame, i have the following positions in latitude and longitude for the element, for example for frame 0:
- Latitude: 49.011213
- Longitude: 8.422885

For frame 1:
- Latitude: 49.01121
- Longitude: 8.422887

Frame (Frame 0) needs to be point (0,0) and I want the following ones to be converted to XY Coordinates as well.

Basically, I need a Javascript script that receives the latitude and longitude of a frame and returns the position (x,y) for that frame (in relation to frame 0 with position (0,0)).

I've tried the following but it doesn't work:

function convertSphericalToCartesian(latitude, longitude)
{
    // Convert from Degrees to Radians
    let latRad = latitude * (Math.PI)/180; 
    let lonRad = longitude * (Math.PI)/180;

    let earthRadius = 6367; // Radius in km
    let posX = earthRadius * Math.cos(latRad) * Math.cos(lonRad);
    let posY = earthRadius * Math.cos(latRad) * Math.sin(lonRad);
    return {x: posX, y: posY};
}

Do you know a better formula for this? Thank you very much.

Toli
  • 13
  • 4

1 Answers1

1

Any conversion you do will introduce error, since you're trying to map the surface of a sphere to rectangular coordinates. You haven't mentioned what you're doing with this information - the mention of "frames" makes me think of animation or a game involving moving things on a map. If that's the case, then your calculations are going to be closely tied to the map you're using.

In general, the question is: how far apart are your coordinates likely to be?

For small distances, up to a few miles or kilometers, you're probably just fine treating the lat/lon as x/y coordinates. For example, the coordinates you've given in your example are literally only feet apart - don't bother with complicated calculations. And again, how you draw something on a given map at those coordinates depends very much on the map you're using.

For larger distances, the curvature of the Earth becomes important and the map projection you're working with will make a big difference in how those coordinates are calculated. For example, this question discusses the calculations for a Mercator projection.

Kryten
  • 15,230
  • 6
  • 45
  • 68
  • I'm using small distances, but it can be anywhere on the globe. However, the map is around 25m² or less. I want a formula for this, since the ones i'm looking online aren't working for me – Toli Mar 12 '20 at 09:36