Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 增加通用转换函数 #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,52 @@
return !(lng > 73.66 && lng < 135.05 && lat > 3.86 && lat < 53.55);
};

/**
* 坐标系转换
* @param {number} lng 经度
* @param {number} lat 纬度
* @param {'wgs84'|'bd09'|'gcj02'} from 来源坐标系名称
* @param {'wgs84'|'bd09'|'gcj02'} to 输出坐标系名称
* @returns {[number,number]} [lng,lat]
*/
function convert(lng, lat, from = 'gcj02', to = 'gcj02') {
let point = [lng, lat];
let arr = {
'wgs84': {
from: (lng, lat) => wgs84togcj02(lng, lat),
to: (lng, lat) => gcj02towgs84(lng, lat)
},
'bd09': {
from: (lng, lat) => bd09togcj02(lng, lat),
to: (lng, lat) => gcj02tobd09(lng, lat)
},
'gcj02': {
from: (lng, lat) => [lng, lat],
to: (lng, lat) => [lng, lat],
}
}
if (!arr[to]) {
console.error('错误的 to 坐标系:' + to);
return point;
}
if (!arr[from]) {
console.error('错误的 from 坐标系:' + from);
return point;
}

if (to == from) {
return point
}
let temp = arr[from].from(lng, lat);
temp = arr[to].to(temp[0], temp[1]);
return temp;
};

return {
bd09togcj02: bd09togcj02,
gcj02tobd09: gcj02tobd09,
wgs84togcj02: wgs84togcj02,
gcj02towgs84: gcj02towgs84
gcj02towgs84: gcj02towgs84,
convert:convert
}
}));
12 changes: 12 additions & 0 deletions test/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ console.log(bd09togcj02);
console.log(gcj02tobd09);
console.log(wgs84togcj02);
console.log(gcj02towgs84);
//百度经纬度坐标转国测局坐标
bd09togcj02 = coordtransform.convert(116.404, 39.915,'bd09','gcj02');
//国测局坐标转百度经纬度坐标
gcj02tobd09 = coordtransform.convert(116.404, 39.915,'gcj02','bd09');
//wgs84转国测局坐标
wgs84togcj02 = coordtransform.convert(116.404, 39.915,'wgs84','gcj02');
//国测局坐标转wgs84坐标
gcj02towgs84 = coordtransform.convert(116.404, 39.915,'gcj02','wgs84');
console.log(bd09togcj02);
console.log(gcj02tobd09);
console.log(wgs84togcj02);
console.log(gcj02towgs84);
//result
//bd09togcj02: [ 116.39762729119315, 39.90865673957631 ]
//gcj02tobd09: [ 116.41036949371029, 39.92133699351021 ]
Expand Down
12 changes: 12 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ <h1>请按F12打开控制台查看结果!(Please open console by F12!)</h1>
console.log(gcj02tobd09);
console.log(wgs84togcj02);
console.log(gcj02towgs84);
//百度经纬度坐标转国测局坐标
bd09togcj02 = coordtransform.convert(116.404, 39.915,'bd09','gcj02');
//国测局坐标转百度经纬度坐标
gcj02tobd09 = coordtransform.convert(116.404, 39.915,'gcj02','bd09');
//wgs84转国测局坐标
wgs84togcj02 = coordtransform.convert(116.404, 39.915,'wgs84','gcj02');
//国测局坐标转wgs84坐标
gcj02towgs84 = coordtransform.convert(116.404, 39.915,'gcj02','wgs84');
console.log(bd09togcj02);
console.log(gcj02tobd09);
console.log(wgs84togcj02);
console.log(gcj02towgs84);
//result
//bd09togcj02: [ 116.39762729119315, 39.90865673957631 ]
//gcj02tobd09: [ 116.41036949371029, 39.92133699351021 ]
Expand Down