So finding the distance between two points in a plane and between two points on Earth is a pretty easy google search away, but converting one to the other I couldn't find how to do.
Using these two functions
function dist = (lat1: number, lng1: number, lat2: number, lng2: number) => {
return Math.sqrt((lat1 - lat2) * (lat1 - lat2) + (lng1 - lng2) * (lng1 - lng2))
}
function distOnEarthInMeters = (lat1: number, lng1: number, lat2: number, lng2: number) => {
if (lat1 === lat2 && lng1 === lng2) return 0
const radianLat1 = lat1 * (Math.PI / 180)
const radianLng1 = lng1 * (Math.PI / 180)
const radianLat2 = lat2 * (Math.PI / 180)
const radianLng2 = lng2 * (Math.PI / 180)
const earthRadius = 6378137
const diffLat = radianLat1 - radianLat2
const diffLng = radianLng1 - radianLng2
const sinLat = Math.sin(diffLat / 2)
const sinLng = Math.sin(diffLng / 2)
const a = Math.pow(sinLat, 2.0) + Math.cos(radianLat1) * Math.cos(radianLat2) * Math.pow(sinLng, 2.0)
return Math.floor(earthRadius * 2 * Math.asin(Math.min(1, Math.sqrt(a))))
}
I get these:
dist(-27, -42, -26, -41) === 1.4142135623730951
distOnEarthInMeters(-27, -42, -26, -41) === 149386
But can can I turn one into the other without knowing the coordinates?
make 1.4142135623730951 become 149386 and 1.4142135623730951 become 149386
(copied to https://math.stackexchange.com/questions/4489148)
As others said this is a math problem. But the answer is no you can't find it. Every map projection changes some distances on the globe. Some projections can conserve some distances, but never all.