Skip to content

Instantly share code, notes, and snippets.

@mladenp
Forked from onderaltintas/degrees2meters.js
Created May 29, 2017 14:48
Show Gist options
  • Save mladenp/ad81f8dbf32c318e221b902abc158575 to your computer and use it in GitHub Desktop.
Save mladenp/ad81f8dbf32c318e221b902abc158575 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