Skip to content

Instantly share code, notes, and snippets.

@arashmad
Last active December 13, 2020 08:19
Show Gist options
  • Save arashmad/65b0248121147d0bf6dd4e797a32e6de to your computer and use it in GitHub Desktop.
Save arashmad/65b0248121147d0bf6dd4e797a32e6de to your computer and use it in GitHub Desktop.
A conversion from Geographic (Latitude & Longitude) to Web Mercator (X & Y)
_latLong2Merc = (_lat, _lon) => {
/*
_lat => Latitude in decimal degree (Number)
_lon => Longitude in decimal degree (Number)
*/
var r_major = 6378137.000
var lon_rad = (_lon / 180.0 * Math.PI)
var x = r_major * lon_rad
var scale = x / _lon
var y = 180.0 / Math.PI * Math.log(Math.tan(Math.PI / 4.0 + _lat * (Math.PI / 180.0) / 2.0)) * scale
return ({ x, y });
}
@arashmad
Copy link
Author

Results were tested by PostGIS ST_Transform function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment