Skip to content

Instantly share code, notes, and snippets.

@gbertb
Forked from onderaltintas/degrees2meters.js
Created December 8, 2016 11:04
Show Gist options
  • Select an option

  • Save gbertb/f9378bb63239955321e703c101782d08 to your computer and use it in GitHub Desktop.

Select an option

Save gbertb/f9378bb63239955321e703c101782d08 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