formatToGPS: Function(val: number, lnglat: 'lng'|'lat') : string
传入十进制经纬度数字, 返回带有字母前缀的度分秒格式经纬度, 保留6位小数
没有做传入最大最小值校验, 有需要的自己添加
// 十进制 => 度分秒
formatToGPS = (val, lnglat) => {
if (typeof val !== 'number') {
return '数据格式不正确';
}
const map = {
lng: ['E ', 'W '],
lat: ['N ', 'S '],
}
const prefix = map[lnglat][val > 0 ? 0 : 1]
val = Math.abs(val).toString()
const split1 = val.split('.');
const degree = split1[0];
const split2 = (`0.${split1[1]}` * 60).toString().split('.');
const minute = split2[0];
const second = (`0.${split2[1]}` * 60).toFixed(6) * 1;
return `${prefix}${degree}°${minute}′${second}″`;
}
formatToGPS(34.98237591, 'lat') // N 34°58′56.553276″