有人知道如何将GPS度转换为十进制值,反之亦然吗?
我必须开发一种方式,用户可以插入地址并获取GPS值(程度和/或小数),但我需要知道的主要是如何转换值,导致用户也可以插入GPS值(度或小数)。因为我需要从谷歌地图获取地图,这需要十进制。
我尝试了一些代码,但我得到了大数字...就像这个:
function ConvertDMSToDD(days, minutes, seconds, direction) {
var dd = days + minutes/60 + seconds/(60*60);
//alert(dd);
if (direction == "S" || direction == "W") {
dd = '-' + dd;
} // Don't do anything for N or E
return dd;
}
任何人?
谢谢。
答案 0 :(得分:7)
首先感谢@Eugen Rieck的帮助。 这是我的最终代码,希望它可以帮助某人:
度到小数
function getDMS2DD(days, minutes, seconds, direction) {
direction.toUpperCase();
var dd = days + minutes/60 + seconds/(60*60);
//alert(dd);
if (direction == "S" || direction == "W") {
dd = dd*-1;
} // Don't do anything for N or E
return dd;
}
根据此link ,十进制到度
function getDD2DMS(dms, type){
var sign = 1, Abs=0;
var days, minutes, secounds, direction;
if(dms < 0) { sign = -1; }
Abs = Math.abs( Math.round(dms * 1000000.));
//Math.round is used to eliminate the small error caused by rounding in the computer:
//e.g. 0.2 is not the same as 0.20000000000284
//Error checks
if(type == "lat" && Abs > (90 * 1000000)){
//alert(" Degrees Latitude must be in the range of -90. to 90. ");
return false;
} else if(type == "lon" && Abs > (180 * 1000000)){
//alert(" Degrees Longitude must be in the range of -180 to 180. ");
return false;
}
days = Math.floor(Abs / 1000000);
minutes = Math.floor(((Abs/1000000) - days) * 60);
secounds = ( Math.floor((( ((Abs/1000000) - days) * 60) - minutes) * 100000) *60/100000 ).toFixed();
days = days * sign;
if(type == 'lat') direction = days<0 ? 'S' : 'N';
if(type == 'lon') direction = days<0 ? 'W' : 'E';
//else return value
return (days * sign) + 'º ' + minutes + "' " + secounds + "'' " + direction;
}
alert(getDD2DMS(-8.68388888888889, 'lon'));
`
答案 1 :(得分:0)
经过一段时间后,无法使上面的脚本正常工作; 只需将dms赋予函数
function ConvertDMSToDEG(dms) {
var dms_Array = dms.split(/[^\d\w\.]+/);
var degrees = dms_Array[0];
var minutes = dms_Array[1];
var seconds = dms_Array[2];
var direction = dms_Array[3];
var deg = (Number(degrees) + Number(minutes)/60 + Number(seconds)/3600).toFixed(6);
if (direction == "S" || direction == "W") {
deg = deg * -1;
} // Don't do anything for N or E
return deg;
}
反之亦然,只是给出脚本的度数,纬度(纬度)为假
function ConvertDEGToDMS(deg, lat) {
var absolute = Math.abs(deg);
var degrees = Math.floor(absolute);
var minutesNotTruncated = (absolute - degrees) * 60;
var minutes = Math.floor(minutesNotTruncated);
var seconds = ((minutesNotTruncated - minutes) * 60).toFixed(2);
if (lat) {
var direction = deg >= 0 ? "N" : "S";
} else {
var direction = deg >= 0 ? "E" : "W";
}
return degrees + "°" + minutes + "'" + seconds + "\"" + direction;
}
希望这对人们有帮助。