如何在jquery或javascript和PHP中将GPS度转换为十进制,反之亦然?

Ped*_*res 5 javascript jquery gps

有人知道如何将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;
}
Run Code Online (Sandbox Code Playgroud)

任何人?

谢谢.

Ped*_*res 8

首先,谢谢你@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;
}
Run Code Online (Sandbox Code Playgroud)

基于此链接的十进制度数

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'));
Run Code Online (Sandbox Code Playgroud)

`