Is there a way to convert coordinates from EPSG:4326 to EPSG:3857 in OpenLayers. Or does any one have a method that does this in Javascript?
-
1Yes, there is a way. For any more you should provide relevant existing code where you want to do conversion.– TomazicMCommented Aug 4, 2020 at 7:40
-
I have 2 objects that i need to compare with each other. The coordinates are just parameters inside those objects. So I don't know what code to at?– StephenCommented Aug 4, 2020 at 7:55
-
Have a look at openlayers.org/en/latest/apidoc/module-ol_proj.html#.toLonLat and openlayers.org/en/latest/apidoc/module-ol_proj.html#.transform.– TomazicMCommented Aug 4, 2020 at 9:07
Add a comment
|
2 Answers
You can use proj4js to convert any coordinate system.
Using EPSG code
proj4("EPSG:4326", "EPSG:3857", [2,5]);
Using projestion code
var firstProjection = 'PROJCS["NAD83 / Massachusetts Mainland",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],UNIT["metre",1,AUTHORITY["EPSG","9001"]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["standard_parallel_1",42.68333333333333],PARAMETER["standard_parallel_2",41.71666666666667],PARAMETER["latitude_of_origin",41],PARAMETER["central_meridian",-71.5],PARAMETER["false_easting",200000],PARAMETER["false_northing",750000],AUTHORITY["EPSG","26986"],AXIS["X",EAST],AXIS["Y",NORTH]]';
var secondProjection = "+proj=gnom +lat_0=90 +lon_0=0 +x_0=6300000 +y_0=6300000 +ellps=WGS84 +datum=WGS84 +units=m +no_defs";
//I'm not going to redefine those two in latter examples.
proj4(firstProjection,secondProjection,[2,5]);
This code converts the coordinates in Javascript
convertCoordinates(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];
}
-
1