Home > ArcGIS, JavaScript > JavaScript: How to convert latitude and longitude to Mercator coordinates

JavaScript: How to convert latitude and longitude to Mercator coordinates

     Continuing the previous post about converting Mercator Sphere coordinates to latitude and longitude, I’d like to show an inverse JavaScript function for converting latitude and longitude to Mercator coordinates:

function LatLonToMercator(lat, lon) {

    var rMajor = 6378137; //Equatorial Radius, WGS84
    var shift  = Math.PI * rMajor;
    var x      = lon * shift / 180;
    var y      = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
    y = y * shift / 180;
    
    return {'X': x, 'Y': y};
}

The use of it is very simple:

var XY = LatLonToMercator(38.878586, -76.989626);
alert('Mercator X:' + XY.X + ', Mercator Y:' + XY.Y);
Related posts:
 
  1. No comments yet.
  1. No trackbacks yet.