Skip to content

Instantly share code, notes, and snippets.

@jibbo
Forked from onderaltintas/degrees2meters.js
Last active September 25, 2018 05:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jibbo/f7dfca9dad07dd860c1b to your computer and use it in GitHub Desktop.
Save jibbo/f7dfca9dad07dd860c1b to your computer and use it in GitHub Desktop.
var degrees2meters = function(lon,lat) {
var x = lon * 20037508.34 / 180;
var y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
y = y * 20037508.34 / 180;
return [x, y]
}
lon= -77.035974
lat = 38.898717
console.log(degrees2meters(lon,lat))
// should result in: -8575605.398444, 4707174.018280
var meters2degress = function(x,y) {
var lon = x * 180 / 20037508.34 ;
var lat =Number(180 / Math.PI * (2 * Math.atan(Math.exp(y * Math.PI / 180)) - Math.PI / 2));
return [lon, lat]
}
x= -8575605.398444
y = 4707174.018280
console.log(meters2degress(x,y))
//should result in -77.035974 38.898717 but gives result -88.42920367320511 lat value. How to inverse that deym function?
//according to stack overflow lat value is => ll.Latitude = 180 / Math.PI * (2 * Math.Atan(Math.Exp(ll.Latitude * Math.PI / 180)) - Math.PI / 2);
//however how to deal with infinity at Math.Exp(ll.Latitude * Math.PI / 180)?
//toFixed will cause precision lose :( there can be recursive method which divide double variable for an array object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment