功能:坐标系转换。
使用方法
proj4(fromProjection[, toProjection, coordinates])
示例代码
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<title>proj4</title>
<script src="js/proj4.js"></script>
</head>
<body>
<script>
// 坐标转换
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.
console.log('NAD83->WGS84 ' + proj4(firstProjection, secondProjection, [2, 5]));
// [-2690666.2977344505, 3662659.885459918]
// 从WGS84转换到firstProjection
console.log('WGS84->NAD83 ' + proj4(firstProjection, [-71, 41]));
// [242075.00535055372, 750123.32090043]
// 不提供坐标
console.log('NAD83->WGS84 ' + proj4(firstProjection, secondProjection).forward([2, 5]));
// [-2690666.2977344505, 3662659.885459918]
console.log('NAD83->WGS84 ' + proj4(secondProjection, firstProjection).inverse([2, 5]));
// [-2690666.2977344505, 3662659.885459918]
console.log('WGS84->NAD83 ' + proj4(firstProjection).forward([-71, 41]));
// [242075.00535055372, 750123.32090043]
console.log('NAD83->WGS84 ' + proj4(firstProjection).inverse([242075.00535055372, 750123.32090043]));
//[-71, 40.99999999999986]
// 定义转换
proj4.defs('WGS84', "+title=WGS 84 (long/lat) +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees");
proj4.defs([
[
'EPSG:4326',
'+title=WGS 84 (long/lat) +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees'],
[
'EPSG:4269',
'+title=NAD83 (long/lat) +proj=longlat +a=6378137.0 +b=6356752.31414036 +ellps=GRS80 +datum=NAD83 +units=degrees'
]
]);
// proj4('EPSG:4326');
// 内置:WGS84/EPSG:4326, EPSG:4269, EPSG:3857/EPSG:3785/GOOGLE/EPSG:900913/EPSG:102113/
// 重定义:proj4.defs('urn:x-ogc:def:crs:EPSG:4326', proj4.defs('EPSG:4326'));
</script>
</body>
</html>