Skip to content

Instantly share code, notes, and snippets.

@springmeyer
Last active August 22, 2024 14:22
Show Gist options
  • Save springmeyer/871897 to your computer and use it in GitHub Desktop.
Save springmeyer/871897 to your computer and use it in GitHub Desktop.
convert from long/lat to google mercator (or EPSG:4326 to EPSG:900913)
// See https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames for more details.
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]
}
x= -77.035974
y = 38.898717
console.log(degrees2meters(x,y))
// should result in: -8575605.398444, 4707174.018280
@subhasreem
Copy link

Thank you so much! It was really helpful.

@rendrom
Copy link

rendrom commented Mar 7, 2020

console.log(degrees2meters(-180,-90)) //  [-20037508.34, -Infinity]

@Us77
Copy link

Us77 commented Mar 24, 2020

Can you please help with converting meters to lat long?

@kb173
Copy link

kb173 commented Apr 14, 2020

@Us77:

The C++ macro posted above works well for converting meters to lat/long degrees: #define DEGREE_TO_METER_REVERSE(Y) (atan(pow(M_E, ((Y)/111319.490778)*M_PI/180.0))*360.0/M_PI-90.0) (Thanks @jiangfeng79!)

Should be trivial to adapt to Python.

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